xcode 如何在 Three20 的 AppDelegate 中添加全局右栏按钮(UINavigationController)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8388566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a global right bar button (UINavigationController) in Three20's AppDelegate
提问by Howard
I want to add a global right bar button in the global AppDelegate so all my view controllers will have this button automatically.
我想在全局 AppDelegate 中添加一个全局右栏按钮,这样我的所有视图控制器都会自动拥有这个按钮。
I added in the AppDelegate
我在 AppDelegate 中添加
navigator.window.rootViewController.navigationController.navigationItem.rightBarButtonItem
= [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Test", @"")
style:UIBarButtonItemStyleBordered target:self action:@selector(showTest)] autorelease];
Of course the above code is not working..any problem with the code above?
当然上面的代码不起作用..上面的代码有什么问题吗?
回答by aporat
Well, I'm not sure you can do it in your way, because UINavigatorController
always uses the buttons from the view controller that is currently displayed, and not from the top / root controller.
好吧,我不确定您是否可以按照自己的方式进行操作,因为UINavigatorController
始终使用当前显示的视图控制器中的按钮,而不是顶部/根控制器中的按钮。
What you can do is to subclass TTViewController
with a new view controller and set your left bar button item.
您可以做的是TTViewController
使用新的视图控制器进行子类化并设置左栏按钮项。
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation BaseViewController
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIViewController
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Test", @"")
style:UIBarButtonItemStyleBordered target:self action:@selector(showTest)] autorelease];
}
and then you should extend all your view controllers from this base controller, which contains the right navigation bar item
然后你应该从这个基本控制器扩展你的所有视图控制器,它包含正确的导航栏项目
回答by Yosi Taguri
What you need to do is to tap into the navigation controller you wish to hook into. then you can implement UINavigationControllerDelegate (each navigation controller has a delegate property) which will give you these events:
您需要做的是点击您希望挂钩的导航控制器。然后你可以实现 UINavigationControllerDelegate (每个导航控制器都有一个委托属性),它会给你这些事件:
// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
You can implement
你可以实施
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
and put your right button in place.
并将您的右键放在适当的位置。