如何在Three20的AppDelegate中添加全局右键按钮(UINavigationController)

问题描述:

我想在全局AppDelegate中添加一个全局右键按钮,这样我所有的视图控制器都会自动使用此按钮。如何在Three20的AppDelegate中添加全局右键按钮(UINavigationController)

我在AppDelegate中

当然加入上述代码不是working..any问题与上面的代码?

+0

而据我所知,目前的iPhone SDK的状态是不可能的。您需要将该项目添加到您拥有的每个视图控制器。 –

嗯,我不确定你可以这样做,因为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]; 
} 

,然后你应该继承从这个基本控制器,它包含右侧导航栏项目

+0

谢谢。但是因为我有一个始终出现在应用程序中的UITabBarController,为什么当我将代码添加到我的UITabBarController的viewDidLoad中时,该按钮无法显示? – Howard

+0

因为UITabBarController没有导航栏。只有UINavigationController确实提供了一个导航栏,因此也提供了导航栏按钮。即使您可以将按钮项放置在界面构建器的导航栏中。这只是一个模拟,并不会以这种方式显示在您的应用程序中。 比较: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html http://developer.apple.com/library/ios/ #documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html – rockstarberlin

你需要做的是挖掘到你想挂接到导航控制器的所有视图控制器。那么你就可以实现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;

,并把你的右键到位。

+1

这似乎是最好的解决方案,因为它不需要子类化,如果您想使用各种UIView类型,这是限制性的。 –

+0

我使用这些更改导航控制器的viewcontrollers的样式更改应用程序主题。很有用。 –

+0

您是否将上面的代码添加到根控制器的每个视图控制器?我试图将代码添加到导航控制器的类,但它不起作用。 –