【问题标题】:How to add a global right bar button (UINavigationController) in Three20's AppDelegate如何在 Three20 的 AppDelegate 中添加全局右栏按钮(UINavigationController)
【发布时间】:2011-12-05 16:35:12
【问题描述】:

我想在全局 AppDelegate 中添加一个全局右栏按钮,这样我的所有视图控制器都会自动拥有这个按钮。

我在 AppDelegate 中添加了

navigator.window.rootViewController.navigationController.navigationItem.rightBarButtonItem     
= [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Test", @"") 
style:UIBarButtonItemStyleBordered target:self action:@selector(showTest)] autorelease]; 

上面的代码当然不行。。上面的代码有什么问题吗?

【问题讨论】:

  • 而且,据我所知,在 iPhone SDK 的当前状态下无法做到这一点。您需要将该项目添加到您拥有的每个视图控制器中。

标签: iphone objective-c ios xcode three20


【解决方案1】:

好吧,我不确定您是否可以按照自己的方式进行操作,因为 UINavigatorController 始终使用当前显示的视图控制器中的按钮,而不是来自顶部/根控制器的按钮。

您可以做的是使用新的视图控制器继承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];
}

然后你应该从这个基础控制器扩展你所有的视图控制器,它包含正确的导航栏项目

【讨论】:

【解决方案2】:

您需要做的是点击您希望连接的导航控制器。然后您可以实现 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;

你可以实现

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

然后把你的右键放在适当的位置。

【讨论】:

  • 这似乎是最好的解决方案,因为它不需要子类化,如果你想使用各种 UIView 类型,这是有限制的。
  • 我使用这些来在更改应用程序主题时更新导航控制器的视图控制器样式。非常有用。
  • 你的意思是把上面的代码添加到根导航控制器的每个视图控制器中吗?我试图只将代码添加到导航控制器的类中,但它不起作用。
  • 你把这些代码行放在哪里了?在每个视图控制器中? @RupertRawnsley
  • @ChenyaZhang 实现一个 UINavigationControllerDelegate 类并使用 setDelegate 告诉你的 UINavigationController。然后,实际添加按钮的代码将存在于您在自定义 UINavigationControllerDelegate 中创建的 willShowViewController 方法中。
猜你喜欢
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多