【问题标题】:Change View Controller with another view Controller swift用另一个视图控制器快速更改视图控制器
【发布时间】:2016-03-11 09:54:23
【问题描述】:

假设我有名为 A、B、C、D 的视图控制器。我使用 push 从 A -> B -> C 开始。现在我想从 C 转到 D,当我从 D 按下返回按钮时,它应该去A。我该怎么做?有什么建议么?

【问题讨论】:

  • 使用这个-> self.navigationController?.popToRootViewControllerAnimated(true)
  • 但它会带我到 A.. 我想直接从 C 到 D.. 它应该弹出所有视图控制器。

标签: ios swift uinavigationcontroller push


【解决方案1】:

您需要使用自定义按钮修改您的后退按钮,并添加一个操作,该操作会将您弹出到您当前所在的UINavigationController 内的 rootViewController。 (popToRootViewController 表示从导航堆栈中删除所有其他控制器并转到第一个)

在你的最后一个 viewController 上放这些行:

目标-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];
}

斯威夫特

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

【讨论】:

  • 函数中的self.navigationController?. popToRootViewControllerAnimated(true) 应该用来代替self.navigationController?.popViewControllerAnimated(true)
  • @RiddhiShah 是的,这里有一个错字,谢谢您的指出!更新了:)
【解决方案2】:

如果要pop到root(A),例如D --> A(直接)

 self.navigationController?.popToRootViewControllerAnimated(true)

如果你想回到之前的视图控制器,例如:D --> C or c--> B or B to A

 self.navigationController?.popViewControllerAnimated(true)

【讨论】:

    【解决方案3】:

    使用UINavigationController,您可以调用popToRootViewControllerAnimated(_:),它将带您回到您的堆的第一个控制器。

    所以使用 NavController,将 A 设置为 root,在 D 中设置popToRootViewControllerAnimated(true)

    https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/popToRootViewControllerAnimated:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多