14

这似乎不起作用。我究竟做错了什么?

-(void)awakeFromNib{
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showNewEventViewController)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    NSLog(@"awaked");
    [rightBarButtonItem release];
}
4

3 回答 3

22

我的猜测是,你添加UIBarButtonItem到错误的对象!您需要将它添加到 rootViewController (而不是UINavigationController像您可能所做的那样)

YourRootViewController *theRootController = [[YourRootViewController alloc] init];

UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];

UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(dismiss)];    
theRootController.navigationItem.rightBarButtonItem = btnCancel

[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
于 2012-02-09T21:06:57.753 回答
11

我通常会将这段代码放在viewDidLoad方法而不是awakeFromNib方法中;我不确定这是否是您的问题所在。“不工作”是什么意思?

于 2010-01-19T01:08:28.693 回答
3

试试这个:

- (void) initUI {   
   UIBarButtonItem *btnCancel = [[[UIBarButtonItem alloc] initWithTitle:@"Cancel" 
                                  style:UIBarButtonItemStyleBordered 
                                  target:self 
                                  action:@selector(dismiss)]autorelease];    

   self.navigationItem.rightBarButtonItem = btnCancel;

   //[btnCancel release]; no need to explicitly release the item

}
于 2010-11-13T10:30:52.707 回答