简体   繁体   English

如何从链接的故事板场景向父UINavigationController添加按钮?

[英]How to add buttons to a parent UINavigationController from a linked storyboard scene?

My setup 我的设定

In a main storyboard i'm using a SWRevealViewController for a sidebar with links to different sections. 在主故事板上,我将SWRevealViewController用于带有链接到不同部分的侧边栏。

Each section is represented in a separated storyboard linked in the main storyboard with RBStoryboardLink . 每个部分都在一个单独的故事板中表示,该故事板在主故事板中与RBStoryboardLink链接在一起。

If in the child storyboards i perform segues to scenes the main navigation controller it's able to represents this putting the destination scene title and a back button. 如果在子情节提要中我对主要导航控制器执行场景定位,则可以将目标场景标题和后退按钮表示为场景。 At this point all works well. 至此,一切正常。

An aproximation to the schema is: 模式的近似值是:

-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

My problem 我的问题

I can't add rightBarButtonItems to the main navigation bar from the child storyboards. 我无法从子情节提要板上将rightBarButtonItems添加到主导航栏中。

What i have tried but without success 我尝试过但没有成功的

Dynamically add barbuttons using singleton 使用单例动态添加条形按钮

I had made the main navigationcontroller a singleton with a method for add a button passed from the child scenes of the differents storyboards. 我已经使主导航控制器成为一个单例,并具有一种方法,用于添加从不同故事板的子场景传递的按钮。

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

This only works at the first time, even when on the menu the root scene is called. 即使仅在菜单上调用了根场景,这也仅在第一次使用时有效。

Narrow the main navigationbar 缩小主导航栏

The worst solution, also don't work. 最糟糕的解决方案也不起作用。 I'm not able to change the main navigationbar width in order to reveal the child scenes top buttons. 我无法更改主导航栏的宽度以显示子场景的顶部按钮。

I've solved my problem in this no elegant way: 我已经用一种不太优雅的方式解决了我的问题:

In the CommonContainer or specific container i've added the nagivation items desired, and associate a IBAction target that send a message received by the specific UIViewControllers of the desired storyboard. 在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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM