Accessing Button Created in Subclassed UITabBarController
Ok, so I'm a little stuck and maybe someone can lend some advice.
I've subclassed UITabBarController, and am creating a custom button that overlays the tab bar whenever viewDidLoad
gets called inside the CustomTabBarController
.
This works great, except its not tied to any action.
What I would like to do is to have a UIModalViewController
be displayed when that button is pressed. Now, preferably I would rather not make this call from the subclassed CustomTabBarController
, but rather from within one of my viewControllers (rootViewController
per-say) that is associated with a tab.
Can someone direct me in how to make this happen? IE, How to instantiate a button in one class and make that button respond开发者_如何学编程 to an action within another class.
Should I use NSNotificationCenter
, delegate responders, something else? An example would be great :)
There are several approaches to achieve what you're asking for. The approach I usually take is that I do something like this:
// CustomTabBarController.h
@protocol CustomTabBarControllerDelegate
- (void)buttonAction:(id)sender;
@end
@interface CustomTabBarController : UITabBarController {
id<CustomTabBarControllerDelegate> customDelegate;
}
@property(nonatomic, assign) id<CustomTabBarControllerDelegate> customDelegate;
@end
// CustomTabBarController.m
@interface CustomTabBarController ()
- (void)buttonAction:(id)sender;
@end
@implementation CustomTabBarController
@synthesize customDelegate;
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Configuration of button with title and style is left out
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(id)sender {
[self.customDelegate buttonAction:sender];
}
@end
I don't know what you're button will be doing, so I just call the method buttonAction:
. Since UITabBarController
already has a property named delegate
I called our delegate customDelegate
. All you need to do to make the above work is to add the following line to your root view controller (or whatever controller you want to handle the button action).
customTabBar.customDelegate = self;
Of course you also have to implement the protocol.
One could also imagine not using a delegate and just set the target like this:
[button addTarget:self.rootViewController action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
The above code assumes that customTabBarController
has a rootViewController
property and that it has been set. Also it assumes that the root view controller has the following method:
- (void)buttonAction:(id)sender;
I prefer the delegate approach as it is the more general approach, but the later approach will also work. Using NSNotificationCenter
is also an option, but I'm personally not a fan of sending notifications when it isn't necessary. I usually only use notifications when multiple objects need to respond to an event.
You can reference all the view controller in your UITabBarController
with the viewControllers
array. You can easily get view controller for the currently selected view with selectedViewController
.
With that in mind, your CustomTabBarController
action can call a methods on these view controllers. Just add method to the appropriate view controller(s) to display your UIModalViewController
.
精彩评论