How to import IBActions from other .h file?
I have many view controllers on my iOS app and in most of them I need the exact same IBAction.
So instead of writing the same code in all view controllers, is it possible to create a separated .h file with the IBActions and then import on the viewcontroller.h?
I tried that creating a Actions.h file and importing it with the command "#import Actions.h" on the ViewController.h file, but when I go to the Interface Builder the actions doesn't sho开发者_开发知识库w up. Is there any way to do this???
Define the method in the superclass:
@interface MyActionViewController: UIViewController
{
}
- (IBAction)theAction:(id)sender;
@end
Subclass your view controllers from it:
@interface FirstViewController: MyActionViewController
{
}
@end
Not import, you need to inherit. Create a viewController with that IBAction and inherit that in every viewControllers that you would like to use.
You can actually do it all in Interface Builder as well. You can do it with the option to add an "Object" to your nib file. In the palette with all of the interface objects, there is one with an orange-colored cube labelled "Object".
Put that one on the root-level of your view hierarchy.
Change the custom class to the name of your class (sounds like it is "Actions"). Once you have changed the name from Object to the name of the class, Interface Builder will let you access any of the IBAction on that class.
Connect the button to this custom class's IBAction.
You can do the same thing for any of the other nib files which need to use this button code, and it saves you from having to subclass.
If anyone is trying to do this on Swift 2, this is how:
import UIKit
class Actions: UIViewController {
@IBAction func button1(sender: AnyObject) {
}
}
Now the ViewController:
import UIKit
class ViewController: Menu_Swipes {
}
精彩评论