cw9
cw9

Reputation: 145

How to get the tabController from subview in tabs

I have a Class: myTabController, in this class I have a UITabBarController, which has 4 subviews in this UITabBarController.

Now I am in my first subview, say it's tab1, I can call self.parentViewController to get the UITabBarController, and this UITabBarController is owned by myTabController, but How can I get myTabController then? Cause I want to access some data in myTabController.

Thanks Ahead, Regards

Upvotes: 0

Views: 798

Answers (1)

Alex Nichol
Alex Nichol

Reputation: 7510

From your wording I am assuming that you have not subclassed UITabBarController. I would suggest having a property on all four view controllers, something like theTabController, which points to an instance of your class. Declare this property like this (in the subviews):

@class myTabController;
...
@interface MySubview : UIView {
    ...
    myTabController * theTabController;
    ...
}
...
@property (nonatomic, assign) myTabController * theTabController;

Then, in each subview's implementation, add a synthesize statement. It's also a good idea to import the header of myTabController in the .m, even though we have an @class in the subview's header. I used an @class to prevent circular imports.

#import "myTabController."
...
@implementation MySubview
@synthesize theTabController;
...
@end

From myTabController, you need to set this property on each subview like this:

subview1.theTabController = self;
subview2.theTabController = self;
...
subviewx.theTabController = self;

Finally, use the theTabController property sub inside each subview with self.theTabController.

I also have to point out: it's never good to have a class name that starts with a lower case letter. myTabController should really be MyTabController.

Upvotes: 2

Related Questions