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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16057327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:13:16  来源:igfitidea点击:

how to create back button in navigation bar

iosobjective-cuibuttonuinavigationbar

提问by janatan

I have 2 page. first is tableView and second is view when I to click on any cell go on to next page (view) in way modal segue. I want add back button in next page of navigation bar . this is my code in view page : ViewController.m

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

回答by Lochana Ragupathy

As you said in your comment you use a modal controller

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

Add the following in viewWillappear

viewWillappear 中添加以下内容

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

And in

而在

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

回答by Shoaib Bagwan

Swift 3

斯威夫特 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) }

回答by Evdzhan Mustafa

I had similar issue, but I am using Swift. Here is the answer in Swift 2.2.

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