当没有后退按钮时,向左侧栏按钮项添加到UINavigationController

时间:2014-02-19 18:25:47

标签: ios iphone objective-c uiviewcontroller uinavigationcontroller

我想在导航栏中添加默认的左栏按钮项。它应该仅在UINavigationController没有提供后退按钮时显示。

最好的方法是什么?

5 个答案:

答案 0 :(得分:33)

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if(navigationController.viewControllers.count != 1) { // not the root controller - show back button instead
        return;
    }
    UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
                                                                              target:self
                                                                              action:@selector(menuItemSelected:)];   
    [viewController.navigationItem setLeftBarButtonItem:menuItem]; 
}

答案 1 :(得分:3)

Swift 3版本:

let done = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(YourController.done))
navigationItem.setLeftBarButtonItem(done, animated: false)

答案 2 :(得分:2)

添加leftBarButtonItem ...

的Swift代码
    let leftMenuItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "leftMenuItemSelected:");
    self.navigationItem.setLeftBarButtonItem(leftMenuItem, animated: false);

答案 3 :(得分:1)

快捷键4

def _create_multiInput_fcn(vector_fcn,var_list):
    exec('''
def g({0}):
    return vector_fcn([{0}])
'''.format(','.join(var_list)), globals(), locals())
    return g
g = _create_multiInput_fcn(f,var_list)

答案 4 :(得分:1)

对于Swift 4.x和更高版本(5.x)

func addNavigationItemBtn(){
    let backBtn = UIBarButtonItem.init(title: "Menu", style: .plain, target: self, action: #selector(menuAction))
    self.navigationItem.leftBarButtonItem = backBtn
}

@objc func menuAction(){
    print("menu button pressed")  //Do your action here
}