I have a base view controller class which inherits from UIViewController
and an inherited class from the base viewcontroller
class. Now I have a nib whose file owner is the inherited class but all my actions and outlets are in the base class. Is it even possible to connect the action\outlet in the nib file to that in the base class?
An IBOutlet is for hooking up a property to a view when designing your XIB. An IBAction is for hooking a method (action) up to a view when designing your XIB. An IBOutlet lets you reference the view from your controller code.
If you have a property defined that you want to make accessible to your storyboards, just add the @IBOutlet attribute before your property. Similarly with @IBAction to connect storyboard actions back to code. class MyViewController: UIViewController { @IBOutlet weak var likeButton: UIButton?
IBAction and IBOutlet are interface Builder Constants IBOutlet:A Controller class can refer to the object in the nib file using a special constant called IBOutlet. IBActions:Interface objects in the nib file can be set to trigger specific methods in controller class using IBAction as return type of the method.
I'll be basically explain it by example:
Define a base class (let's call it BaseViewController
) and assign it a UITableView
IBoutlet in the .h file:
// BaseViewController.h
@interface BaseViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
Define two children classes (ie FirstChildViewController
and SecondChildViewController
):
// FirstChildViewController.h
@interface FirstChildViewController : BaseViewController
@end
// SecondChildViewController.h
@interface SecondChildViewController : BaseViewController
@end
Now to make either (or both of) the children classes use the iboutlet of the base class, simply drag the referencing outlet to the property definition in the base class .h file.. and that's it!
here is a complete project that illustrates this.
Two ways to accomplish this:
1)
do it programmatically (i.e. in code)
First declare your outlets in the base class.
and then assign your outlets via code in your inherited class.
and
2)
You can also assign your outlets and actions in your XIB file. Xcode knows about inherited outlets and actions from base classes.
If the base class is in a Swift framework, then sometimes Xcode 7 will not see the outlets, even if the base class and its outlets are public. In this case the workaround is as follows:
IB will show "!" next to the outlets as if they are missing, but there will be no errors at runtime.
If someone has a better workaround, let me know!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With