Erika Electra
Erika Electra

Reputation: 1882

ios - Where to hook up outlets in UIViewController subclass?

I subclassed UIViewController (let's call it ParentViewController) and added a bunch of properties and methods -- IBOutlets and IBActions and such -- that I want to subclass for ChildViewControllerA, ChildViewControllerB, ChildViewControllerC, etc.

My stupid question is, since all of these properties are defined in ParentViewController's interface, how do I hook them up in the ChildViewControllers' interfaces? In my ios 7 storyboard main file, I have a ViewController for each one of ChildViewControllerA, ChildViewControllerB, ChildViewControllerC, etc. but I don't have the little plus-sign ports in their corresponding .h/.m files because they are all in ParentViewController.h.

Is there something I'm missing? Do I need to redefine the properties in each ChildViewController in order to have some place to hook them up to their corresponding storyboard views?

Upvotes: 0

Views: 872

Answers (3)

iphonic
iphonic

Reputation: 12719

The logic is quite simple, if you inherit any class the Outlets will be available to all the other classes which inherits it. For eg.

In your case you have Parent and in it you have Outlet say a ButtonA. Now other classes Child1 and Child2 inherits Parent they can access ButtonsA, but if you create any outlet to Child1 and Child2 and if no other class inherits it, it wont be available for any, and it wont be available for Parent as well.. If you want to access the properties you can have the Child Controller as a property in any one of your class.

Hope this helps.

Upvotes: 0

FluffulousChimp
FluffulousChimp

Reputation: 9185

This should work in the manner your described; as I just verified.

@interface ParentViewController : UIViewController
@property (nonatomic, weak) IBOutlet UILabel *someLabel;
@end

Create subclasses of ParentViewController and set the custom classes in the Storyboard editor to ChildViewControllerA and ChildViewControllerB, like so:

enter image description here

The outlets should be there if this corresponds to what you're doing.

enter image description here

In short, you should not need to redefine outlets that are defined earlier in the object hierarchy.

Upvotes: 2

CyberK
CyberK

Reputation: 1578

What do you try to do? If you try to pass parameters to another view controller, use this method in your ParentViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
     [segue.destinationViewController yourInitializationMethod:withProperties];
}

Upvotes: 0

Related Questions