Add button on UINavigationController
i'm trying to develop my first iphone apps. I'm using a navigationController with three views: main->first view ->second view. I would like to add a button only on the first view, i tried like this:
self.navigationItem.开发者_开发问答rightBarButtonItem = self.editButtonItem;
but nothing appear. If I add the button on the main view it appears after on each view, is possible to do what I want? Thanks!
One more point to add to the answers pointed out by others is that for adding a button on the toolbar please ensure that you are accessing your view controller which is added to the UINavigationController.You should be modifying the view controller like this..
navController=[[UINavigationController alloc] initWithRootViewController:viewController];
[viewController.navigationItem setRightBarButtonItem:rightBarButton];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"YOUR TITLE" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 49, 30)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
精彩评论