iOS navigationBar 添加UIBarButtonItem

方式一:

1.设置按钮
    // 设置左边按钮
    UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarBtnAction:)];
    self.navigationItem.rightBarButtonItem = leftBtnItem;
2.设置跳转到的页面
-(void)rightBarBtnAction:(UIBarButtonItem*)sender{
    // push到下个界面
    CSCanteenListTabVController *secondVC = [[CSCanteenListTabVController alloc]init];
    [self.navigationController pushViewController:secondVC animated:YES];
}

方式二:

1.设置按钮
UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancleButton.frame = CGRectMake(0, 0, 80, 40);
    [cancleButton setTitle:@"撤销" forState:UIControlStateNormal];
    [cancleButton setTitleColor:[UIColor nzq_colorWithHex:0x111111] forState:UIControlStateNormal];
    [cancleButton addTarget:self action:@selector(rightBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:cancleButton];
    self.navigationItem.rightBarButtonItem = rightItem;
2.设置跳转到的页面
-(void)rightBarBtnAction:(UIBarButtonItem*)sender{
    // push到下个界面
    CSCanteenListTabVController *secondVC = [[CSCanteenListTabVController alloc]init];
    [self.navigationController pushViewController:secondVC animated:YES];
}

你可能感兴趣的:(iOS navigationBar 添加UIBarButtonItem)