【问题标题】:How to add buttons to a parent UINavigationController from a linked storyboard scene?如何从链接的故事板场景向父 UINavigationController 添加按钮?
【发布时间】:2014-07-27 16:00:19
【问题描述】:

我的设置

在主情节提要中,我使用SWRevealViewController 作为带有不同部分链接的侧边栏。

每个部分都表示在一个单独的故事板中,并在主故事板中使用RBStoryboardLink链接。

如果在子情节提要中,我对主导航控制器的场景执行 segues,它能够表示这个放置目标场景标题和后退按钮。此时一切正常。

架构的近似值是:

-SWRevealViewController
 |-> (SWRevealViewControllerSegue) -> sw_front
 |-> (SWRevealViewControllerSegue) -> sw_rear

-(sw_rear)UITableViewController
 |(each row selection)
 |-> SWRevealViewControllerSegue -> (CommonContainer)

-CommonContainer
 |-> Container -> Embed segue for container -> RBStoryboardLink

- RBStoryboardLink
 |->(User defined runtime attributes) "storyboardName" : "target_storyboard"

-SpecfificControllerOfTargetedStoryboard
 -(void)viewDidLoad{
    UIBarButtonItem * button = //...
    self.topViewController.navigationItem.rightBarButtonItem = button;//DOESN'T WORK

我的问题

我无法将 rightBarButtonItems 从子情节提要添加到主导航栏。

我尝试过但没有成功

使用单例动态添加条形按钮

我已将主导航控制器设置为单例,其中包含添加从不同情节提要的子场景传递的按钮的方法。

- (void) addRightButton:(UIBarButtonItem *) button{
    self.topViewController.navigationItem.rightBarButtonItem = button;
}

这仅在第一次有效,即使在菜单上调用根场景时也是如此。

缩小主导航栏

最糟糕的解决方案,也不起作用。我无法更改主导航栏宽度以显示子场景顶部按钮。

【问题讨论】:

    标签: objective-c ios7 uinavigationcontroller swrevealviewcontroller


    【解决方案1】:

    我用这种不优雅的方式解决了我的问题:

    在 CommonContainer 或特定容器中,我添加了所需的导航项,并关联了一个 IBAction 目标,该目标发送由所需故事板的特定 UIViewControllers 接收到的消息。

    -CommonContainer
     |-> Container -> Embed segue for container -> RBStoryboardLink
     |-(void)viewDidLoad{
         UIButton *rightButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 15, 13)];
         [rightButton setBackgroundImage:[UIImage imageNamed:@"mv-white-setting.png"] forState:UIControlStateNormal];
         [rightButton setTitle:@"" forState:UIControlStateNormal];
         [rightButton addTarget:self action:@selector(rightTopBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
         UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
         self.navigationController.topViewController.navigationItem.rightBarButtonItem = rightButtonItem;
     }
     |- (IBAction)rightTopBarButtonAction:(id)sender {
         [[NSNotificationCenter defaultCenter] postNotificationName:@"movements_rightTopBarButtonAction" object:nil];
     }
    
    - RBStoryboardLink
     |->(User defined runtime attributes) "storyboardName" : "target_storyboard"
    
    -SpecfificControllerOfTargetedStoryboard
     |-(void)viewDidLoad{
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rightButtonAction:) name:@"movements_rightTopBarButtonAction" object:nil];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      相关资源
      最近更新 更多