Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to connect to IBOutlet or IBAction in base class

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?

like image 556
FatalError Avatar asked Apr 18 '12 22:04

FatalError


People also ask

What is the difference between IBOutlet and IBAction?

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.

How do you declare an IBOutlet property?

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?

What does IBOutlet mean?

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.


3 Answers

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!

enter image description here

here is a complete project that illustrates this.

like image 61
abbood Avatar answered Oct 17 '22 07:10

abbood


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.

like image 21
Michael Dautermann Avatar answered Oct 17 '22 06:10

Michael Dautermann


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:

  1. In IB set the control to use the base class
  2. Connect the outlets
  3. In IB set the control to use the derived class

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!

like image 7
phatmann Avatar answered Oct 17 '22 06:10

phatmann