如何创建早在导航栏按钮(how to create back button in navigatio

2019-08-31 09:15发布

我有2个页面。 第一是的tableView和第二个是视图,当我点击任何单元格,进入下一个页面(view)的方式模态SEGUE。 我想在导航栏上的下一个页面中添加后退按钮。 这是我考虑到页面代码:ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.lable.text = obji.Name;
    self.lable2.text = obji.Descript;

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(Back)];
    self.navigationItem.leftBarButtonItem = backButton;
}

- (IBAction)Back
{
    //I dont know that how return pervious page
}

Answer 1:

当您在您的评论说,您使用的是模态控制器

添加在viewWillAppear中以下

     UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:self action:@selector(Back)];
     self.navigationItem.leftBarButtonItem = backButton;

而在

- (IBAction)Back
  {
    [self dismissViewControllerAnimated:YES completion:nil]; // ios 6
  }


Answer 2:

斯威夫特3

let backButton: UIBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(back))
    self.navigationItem.leftBarButtonItem = backButton

func back() {
    self.dismiss(animated: true, completion: nil) }


Answer 3:

我有类似的问题,但我使用的斯威夫特。 以下是雨燕2.2的答案。

        override func viewWillAppear(animated: Bool) {
            let backButton: UIBarButtonItem = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: #selector(back))
            self.navigationItem.leftBarButtonItem = backButton;
            super.viewWillAppear(animated);
        }

        func back() {
            self.dismissViewControllerAnimated(true, completion: nil)
        }


文章来源: how to create back button in navigation bar