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
how to create back button in navigation bar
提问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)
}