[英]Change View Controller with another view Controller swift
Suppose I have view controllers named A, B, C, D. I am using push to go from A -> B -> C. Now I want to go to D from C and when I press back button from D, it should go to A. How Can I do that?假设我有名为 A、B、C、D 的视图控制器。我正在使用 push 从 A -> B -> C 转到 D。现在我想从 C 转到 D,当我从 D 按下后退按钮时,它应该去A. 我该怎么做? Any suggestions?
有什么建议?
if you want to pop to root (A), for example D --> A (directly)如果你想弹出到根目录(A),例如 D --> A(直接)
self.navigationController?.popToRootViewControllerAnimated(true)
If you want to go back to the previous view controller , for ex : D -- > C or c--> B or B to A如果你想回到上一个视图控制器,例如:D --> C or c--> B or B to A
self.navigationController?.popViewControllerAnimated(true)
You need to modify your back button with a custom one, and add an action which will pop you to the rootViewController inside that UINavigationController
you are currently.您需要使用自定义按钮修改后退按钮,并添加一个操作,该操作会将您弹出到您当前所在的
UINavigationController
内的rootViewController 。 ( popToRootViewController means removing all other controllers from the navigation stack and go the first one) ( popToRootViewController意味着从导航堆栈中删除所有其他控制器并转到第一个)
On your last viewController put these lines:在您的最后一个 viewController 上放置以下几行:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Go to root" style:UIBarButtonItemStylePlain target:self action:@selector(popToRootNavigationViewController:)];
}
- (void)popToRootNavigationViewController:(UIBarButtonItem *)button {
[self.navigationController popToRootViewControllerAnimated:YES];
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Go to root", style: UIBarButtonItemStyle.Plain, target: self, action: "popToRootNavigationViewController:")
}
func popToRootNavigationViewController(sender: UIBarButtonItem) {
self.navigationController?.popToRootViewControllerAnimated(true)
}
Using a UINavigationController
you can call popToRootViewControllerAnimated(_:)
, it will take you back to the first controller of your pile.使用
UINavigationController
你可以调用popToRootViewControllerAnimated(_:)
,它会带你回到你堆的第一个控制器。
So use a NavController, set A as root, and in D, popToRootViewControllerAnimated(true)
所以使用 NavController,将 A 设置为 root,在 D 中,
popToRootViewControllerAnimated(true)
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/popToRootViewControllerAnimated : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/popToRootViewControllerAnimated :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.