简体   繁体   English

使用另一个视图控制器 swift 更改视图控制器

[英]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 上放置以下几行:

Objective-C目标-C

- (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];
}

Swift迅速

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.

相关问题 从另一个View Controller Swift 2.0更改View Controller图像 - Change View Controller Image from another View Controller Swift 2.0 迅速将viewcontroller转移到另一个视图控制器 - Dismissviewcontroller to another view controller in swift 以编程方式快速更改视图控制器 - change view controller programmatically swift 在标签栏控制器中更改视图控制器(快速) - Change View Controller in Tab Bar Controller (Swift) 如何在Swift中从另一个视图控制器更改标签的出口? - How to change a label's outlet from another view controller in Swift? Swift-在不知道类名的情况下在另一个视图控制器中更改变量? - Swift - change variable in another view controller, without knowing the class name? ios如何快速更改其他视图控制器中另一个视图控制器的内容 - ios how to change another view controller's content in different view controller in swift 从Swift中的另一个视图控制器呈现子类视图控制器 - Present subclassed view controller from another view controller in Swift swift:在第一个视图控制器中向上滑动显示另一个视图控制器 - swift : show another view controller on swipe up in first view controller 来自另一个视图控制器的Swift Present View控制器 - Swift Present View Controller From Another View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM