0

我有一个拆分 VC 作为我的应用程序的入口点。详细 VC 是一个 UINavigationController,我想始终隐藏主 VC,以便我只能在工具栏的弹出窗口中使用它。

我的问题是我没有办法将 barButtonItem 从主 VC 放入我的详细 VC。工具栏总是空的(我不得不用self.toolbarHidden = NO;它来强制它显示,因为顶部有一个导航栏)。

我在 UINavigationController (实际的细节 VC)中有以下代码:

-(void) setBarButtonItem:(UIBarButtonItem *)barButtonItem {
  NSLog(@"adding toolbar button: %@", barButtonItem.title);

  UIToolbar *toolbar = [self toolbar];

  NSMutableArray *toolbarItems = [toolbar.items mutableCopy];

  if (_barButtonItem) [toolbarItems removeObject:_barButtonItem];

  if (barButtonItem) [toolbarItems insertObject:barButtonItem atIndex:0];

  _barButtonItem = barButtonItem;
}

我错过了什么吗?我也尝试将它插入导航栏而不是工具栏,但它也没有显示在那里。请询问我在评论中未提供的任何信息。

4

1 回答 1

0

In case anyone else is trying to do the same thing, I am leaving an implementation to have a UINavigationController as the Detail VC in a splitViewController and have the button at the top of each view controller as you navigate through to show/hide the Master VC:

Your Detail VC has to implement the protocol below (so you need to @synthesize the barButtonItem):

@protocol SplitViewBarButtonItemPresenter <NSObject>
@property (nonatomic, strong) UIBarButtonItem *barButtonItem;
@end

You will need to grab and keep the barButtonItem passed from willHideViewController: in your Detail VC, but you have to do it in the Master VC. Use the following in your Master VC:

- (BOOL) splitViewController:(UISplitViewController *)sender
    shouldHideViewController:(UIViewController *)vc
               inOrientation:(UIInterfaceOrientation)orientation {

    return YES;
}

- (void)splitViewController:(UISplitViewController *)svc
     willHideViewController:(UIViewController *)aViewController
          withBarButtonItem:(UIBarButtonItem *)barButtonItem
       forPopoverController:(UIPopoverController *)pc {

    barButtonItem.title = self.title;

    [self splitViewBarButtonItemPresenter].barButtonItem = barButtonItem;

}

- (void)splitViewController:(UISplitViewController *)svc
     willShowViewController:(UIViewController *)aViewController
          withBarButtonItem:(UIBarButtonItem *)barButtonItem
       forPopoverController:(UIPopoverController *)pc {

    [self splitViewBarButtonItemPresenter].barButtonItem = nil;

}

- (id <SplitViewBarButtonItemPresenter>) splitViewBarButtonItemPresenter {

    id detailVC = [self.splitViewController.viewControllers lastObject];

    if ((![detailVC isKindOfClass:[MainDetailVC class]]) || (![detailVC conformsToProtocol:@protocol(SplitViewBarButtonItemPresenter)])) {

        detailVC = nil;
    }

    return detailVC;
}

In each of the view controllers that you want the button to show, use the following code (you can also put this in a class and inherit from it if they are all the same type):

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    UIBarButtonItem *newBtn = [[UIBarButtonItem alloc]
            initWithTitle:@"Show Master MC" // might want a better title
            style:UIBarButtonItemStylePlain
            target:self action:@selector(forceOpenMasterVC)];

    self.navigationItem.rightBarButtonItem = newBtn;

}

-(void)forceOpenMasterVC {
    [((MainDetailVC *) self.navigationController) forceOpenMasterVC];
}

Then in your Detail VC, use the following:

#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
// Above line is needed because of self.barButtonItem.action
// It doesn't leak as far as I can tell

-(void)forceOpenMasterVC {
    // Grab the Master VC
    UIViewController * vc = [[self.splitViewController viewControllers] objectAtIndex:0];

    if (self.barButtonItem) {
        [self.barButtonItem.target
            performSelector:self.barButtonItem.action
            withObject: self.barButtonItem];
    }
}

If anyone has a better way, please include it.

于 2012-07-26T15:09:15.993 回答