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

How*_*ard 0 iphone xcode objective-c three20 ios

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

我在AppDelegate中添加了

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

当然上面的代码不起作用..上面的代码有问题吗?

apo*_*rat 7

好吧,我不确定你能不能这样做,因为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];
}
Run Code Online (Sandbox Code Playgroud)

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


Yos*_*uri 5

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:

// 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;
Run Code Online (Sandbox Code Playgroud)

You can implement

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

and put your right button in place.