如何以编程方式按下UINavigationController中的“后退”按钮

时间:2011-10-04 09:48:06

标签: iphone objective-c ios uiviewcontroller uinavigationcontroller

我在UIViewController内有一个名为FriendsViewController的{​​{1}}。第二个UINavigationController称为UIViewController。当从第一个视图控制器导航到第二个视图控制器时,我想在需要时以编程方式按下FriendsDetailedViewController按钮。怎么做?

6 个答案:

答案 0 :(得分:165)

只需使用

[self.navigationController popViewControllerAnimated:YES]

来自FriendsDetailedViewController的

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

答案 1 :(得分:16)

如果按“后退”按钮就意味着只需要转到上一个视图控制器,就可以打电话:

[self.navigationController popViewControllerAnimated:YES];

答案 2 :(得分:8)

这是swift方法

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

答案 3 :(得分:1)

快捷键5

 self.navigationController?.popViewController(animated: true)

在代码段中的用法:

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

答案 4 :(得分:0)

以下是我在Swift 3中的表现

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

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

答案 5 :(得分:0)

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

在Swift中

self.navigationController?.popViewControllerAnimated(true)

目标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)

在目标C

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

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