如何按“返回”键以编程方式在 UINavigationController 中按钮

发布于 2024-12-08 03:50:42 字数 265 浏览 1 评论 0原文

我在 UINavigationController 中有一个名为 FriendsViewControllerUIViewController 。第二个 UIViewController 称为 FriendsDetailedViewController。从第一个视图控制器导航到第二个视图控制器时,我想在需要时以编程方式按 Back 按钮。如何做到这一点?

I have a UIViewController called FriendsViewController inside a UINavigationController. And a second UIViewController called FriendsDetailedViewController. When navigating from the first view controller to the second, I want to programmatically press the Back button when needed. How to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

虚拟世界 2024-12-15 03:50:42

只需使用

FriendsDetailedViewController 中的[self.navigationController popViewControllerAnimated:YES]

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

注意,它通常返回 UIViewController,如果没有任何内容可弹出,则返回 nil

Simply use

[self.navigationController popViewControllerAnimated:YES]

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

Note that it returns UIViewController on normally, and returns nil if there is nothing to pop.

春风十里 2024-12-15 03:50:42

如果按“后退”按钮只是想转到上一个视图控制器,则可以调用:

[self.navigationController popViewControllerAnimated:YES];

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

[self.navigationController popViewControllerAnimated:YES];
你对谁都笑 2024-12-15 03:50:42

这是快速方法

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

Here is the swift method

if let navController = self.navigationController {
    navController.popViewControllerAnimated(true)
}
闻呓 2024-12-15 03:50:42

Swift 5

 self.navigationController?.popViewController(animated: true)

代码片段中的用法:

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

Swift 5

 self.navigationController?.popViewController(animated: true)

Usage in a code snippet:

func unwindViewController() {
    self.navigationController?.popViewController(animated: true)
}
追星践月 2024-12-15 03:50:42

1) 当您弹出当前的 NavigationController 时,然后

在 Swift 中

self.navigationController?.popViewControllerAnimated(true)

Objective C

[self.navigationController popViewControllerAnimated:YES];

2) 当您返回另一个导航控制器时,然后

在 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)

在 Objective 中C

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

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

1) When you pop in current NavigationController Then

In Swift

self.navigationController?.popViewControllerAnimated(true)

Objective C

[self.navigationController popViewControllerAnimated:YES];

2) When you back another Navigation controller Then

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

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

[self.navigationController pushViewController:ObjectOfPushVC animated:YES];
醉殇 2024-12-15 03:50:42

这是我在 Swift 3 中的做法

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

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

Here is how I did it in Swift 3

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

_ is used to suppress the ugly warning generated by XCode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文