【问题标题】:UIBarButton in toolbar for UINavigationController that is the detail view of a split VCUINavigationController 工具栏中的 UIBarButton,它是拆分 VC 的详细视图
【发布时间】:2012-07-25 20:48:24
【问题描述】:

我有一个拆分 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;
}

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

【问题讨论】:

    标签: objective-c uinavigationcontroller uisplitviewcontroller uitoolbar


    【解决方案1】:

    如果其他人试图做同样的事情,我将留下一个实现,让 UINavigationController 作为 splitViewController 中的 Detail VC,并在您导航到显示/隐藏时在每个视图控制器的顶部都有按钮风投大师:

    您的 Detail VC 必须实现以下协议(因此您需要 @synthesize barButtonItem):

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

    您需要在 Detail VC 中抓取并保留从 willHideViewController: 传递的 barButtonItem,但您必须在 Master VC 中执行此操作。在您的主 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;
    }
    

    在您希望按钮显示的每个视图控制器中,使用以下代码(如果它们都是相同的类型,您也可以将其放在一个类中并从它继承):

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

    然后在您的 Detail VC 中,使用以下内容:

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

    如果有人有更好的方法,请提供。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-21
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      相关资源
      最近更新 更多