ios 如何以编程方式按下 UINavigationController 中的“返回”按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7646014/
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 21:44:14  来源:igfitidea点击:

How to press "Back" button in UINavigationController programmatically

iphoneobjective-ciosuiviewcontrolleruinavigationcontroller

提问by Timur Mustafaev

I have a UIViewControllercalled FriendsViewControllerinside a UINavigationController. And a second UIViewControllercalled FriendsDetailedViewController. When navigating from the first view controller to the second, I want to programmatically press the Backbutton when needed. How to do this?

我有一个UIViewController叫做FriendsViewControllerUINavigationController。一秒钟UIViewControllerFriendsDetailedViewController. 从第一个视图控制器导航到第二个视图控制器时,我想Back在需要时以编程方式按下按钮。这该怎么做?

回答by Mann

Simply use

只需使用

[self.navigationController popViewControllerAnimated:YES]

[self.navigationController popViewControllerAnimated:YES]

from FriendsDetailedViewController. Your view will be popped out i.e. the behavior of back button

来自 FriendsDetailedViewController。您的视图将弹出,即后退按钮的行为

回答by Bartek

If by pressing "Back" button you mean just to go to the previous view controller, you can just call:

如果按“后退”按钮你的意思只是去上一个视图控制器,你可以调用:

[self.navigationController popViewControllerAnimated:YES];

回答by MGM

Here is the swift method

这是快速方法

if let navController = self.navigationController {
    navController.popViewControllerAnimated(true)
}

回答by Jay Mayu

Here is how I did it in Swift 3

这是我在 Swift 3 中的做法

_ = self.navigationController?.popViewController(animated: true)

_is used to suppress the ugly warning generated by XCode.

_用于抑制 XCode 生成的丑陋警告。

回答by Mohit

1) When you pop in current NavigationController Then

1) 当你弹出当前的 NavigationController 然后

In Swift

在斯威夫特

self.navigationController?.popViewControllerAnimated(true)

Objective C

目标 C

[self.navigationController popViewControllerAnimated:YES];

2) When you back another Navigation controller Then

2)当你支持另一个导航控制器然后

In Swift

在斯威夫特

let story = UIStoryboard(name: "Main", bundle: nil)
let pushVC = story.instantiateViewControllerWithIdentifier("PushVC")
let navigation = story.instantiateViewControllerWithIdentifier("homeNavigation") as! UINavigationController
navigation.pushViewController(pushVC!, animated: true)

In Objective C

在目标 C

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"storyBoardName" bundle:nil];
pushVC* ObjectOfPushVC = [storyboard instantiateViewControllerWithIdentifier:@"pushVC"];

[self.navigationController pushViewController:ObjectOfPushVC animated:YES];

回答by Amit

Swift 5

斯威夫特 5

 self.navigationController?.popViewController(animated: true)

Usage in a code snippet:

在代码片段中的用法:

func unwindViewController() {
    self.navigationController?.popViewController(animated: true)
}