如何在导航栏中创建后退按钮

时间:2013-04-17 10:10:55

标签: ios objective-c uibutton uinavigationbar

我有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
}

3 个答案:

答案 0 :(得分:28)

正如您在评论中所说,您使用模态控制器

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
  }

答案 1 :(得分:6)

Swift 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) }

答案 2 :(得分:4)

我有类似的问题,但我使用的是Swift。这是Swift 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)
        }