开发者

trying to programmatically create rightBarButtonItem

This doesn't seem to be working. What am i doing wrong?

-(void)awakeFromNib{
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonS开发者_如何学编程ystemItemAdd target:self action:@selector(showNewEventViewController)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    NSLog(@"awaked");
    [rightBarButtonItem release];
}


my guess is, that you add the UIBarButtonItem to the wrong object! you need to add it, to the rootViewController (instead to the UINavigationController, as you probably did)

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];


I would normally put this code in the viewDidLoad method rather than the awakeFromNib method; I'm not sure if that's where your problem lies. What does "not working" mean?


Try this instead:

- (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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜