简体   繁体   中英

can't set right button of UITabBarController's moreNavigationController

I'm having a very strange problem where I can set all properties of a UITabBarController's moreNavigationController except for the rightBarButtonItem property. I'm guessing it might be because of some bug relating to the customizableViewControllers property disabling all right bar button items. Any ideas how to fix?

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                   target:self 
                                   action:@selector(popViewController)];

    self.customizableViewControllers = nil;
    self.moreNavigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.moreNavigationController.topViewController.navigationItem.title = @"test"; //this works
    self.moreNavigationController.topViewController.navigationItem.leftBarButtonItem = doneButton; // this works
    self.moreNavigationController.topViewController.navigationItem.rightBarButtonItem = doneButton; // this doesn't

Ok the solution was to use the delegate of moreNavigationController

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *) viewController animated:(BOOL)animated {

viewController.navigationItem.rightBarButtonItem = doneButton;

}

Paludis, thanks a lot! This is a fix for now.

 func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        //More navigation controller Bug. The rightBarButton item disappears on pop, in case this is MoreNavControl
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "createProject")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if (self.navigationController == self.tabBarController?.moreNavigationController)
        {
            self.navigationController?.delegate = self
        }
        else
        {
            navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "createProject")
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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