【问题标题】:How do I add a right button to a UINavigationController that pushes another view controller?如何向推送另一个视图控制器的 UINavigationController 添加右键?
【发布时间】:2014-05-06 14:54:01
【问题描述】:

我有一个连接到导航控制器的视图控制器。我想在顶部的导航栏上添加一个右键,它将推动另一个视图控制器。如果我添加右键:

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

这样做的正确方法是什么?

【问题讨论】:

  • 删除此操作:[self forward] 并尝试操作:@selector(forward)
  • MapViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"map"]; [self performSegueWithIdentifier:@"map" sender:self];然后在forward方法中编写这段代码

标签: ios cocoa-touch


【解决方案1】:

在您的代码中声明 UIBarButtonItem 时,action:[self forward]] 行存在问题...将其更改为 @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];
}

【讨论】:

  • 我认为他没有在情节提要中使用导航。所以我们可以使用 self.navigationController pushViewController。相反,[self presentModalViewController:viewController animated:YES];
  • 在第一行中他提到视图嵌入在导航控制器中,他想在右键单击时推送另一个视图控制器
【解决方案2】:

试试这个代码

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2015-10-21
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    相关资源
    最近更新 更多