Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to programmatically create rightBarButtonItem

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

-(void)awakeFromNib{
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showNewEventViewController)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    NSLog(@"awaked");
    [rightBarButtonItem release];
}
like image 385
Sam Jarman Avatar asked Jan 19 '10 01:01

Sam Jarman


2 Answers

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];
like image 93
Flori Avatar answered Oct 01 '22 17:10

Flori


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?

like image 21
Steve Harrison Avatar answered Oct 01 '22 18:10

Steve Harrison