24

我有 2 页。第一个是 tableView,第二个是视图,当我单击任何单元格时,以模态 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
}
4

3 回答 3

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
  }
于 2013-04-17T10:25:29.190 回答
8

斯威夫特 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) }
于 2017-01-09T15:45:56.263 回答
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)
        }
于 2016-03-27T00:53:52.833 回答