我有一个连接到导航控制器的视图控制器。我想在顶部的导航栏中添加一个右键,以推送另一个视图控制器。如果我添加右键:
UIBarButtonItem *Button = [[UIBarButtonItem alloc] initWithTitle:@"Map" style:UIBarButtonItemStylePlain target:self action:[self forward]];
self.navigationItem.rightBarButtonItem = Button;
出现按钮。但是,如果我将以下行添加到forward方法,该按钮将再次消失:
MapViewController *viewController = [self.navigationController.storyboard instantiateViewControllerWithIdentifier:@"map"];
[self.navigationController pushViewController:viewController animated:YES];
这样做的正确方法是什么?
答案 0 :(得分:1)
在您的代码中声明action:[self forward]]
时,行UIBarButtonItem
出现问题...将其更改为@selector(forward)
或尝试此操作
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(showNextVC)];
self.navigationItem.rightBarButtonItem = nextButton;
并在showNextVC:
-(void) showNextVC {
MapViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"map"];
[self.navigationController pushViewController:viewController animated:YES];
}
答案 1 :(得分:0)
试试此代码
UIImage *rightBtnImage = [UIImage imageNamed:@"bulb-plus.png"];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setImage:rightBtnImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0, 0, 26, 35);
[rightButton addTarget:self
action:@selector(rightBarButtonAction)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
rightBarbutton的IBAction方法
- (void)rightBarButtonAction {
[self performSegueWithIdentifier:@"IdeaDetailSegue" sender:self];
}
希望这有帮助。