gpt4 book ai didi

ios - 从 App Delegate swift 执行 Segue

转载 作者:IT王子 更新时间:2023-10-29 05:16:25 26 4
gpt4 key购买 nike

我需要从应用委托(delegate)启动一个 View Controller 。

在 View Controller 之间执行 segue 的方式。

我有一个 if 语句,如果 true 需要显示一个 View Controller ,这在应用程序委托(delegate)中。

我如何从应用委托(delegate)中执行此操作?

最佳答案

c_rath 的回答大部分是正确的,但您不需要将 View Controller 设为 Root View Controller 。事实上,您可以在导航堆栈的顶 View 和任何其他 View Controller 之间触发一个 segue,甚至可以从 App Delegate 进行。例如,要推送 Storyboard View Controller ,您可以这样做:

Swift 3.0 及更高版本

// Access the storyboard and fetch an instance of the view controller
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: MainViewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! MainViewController;

// Then push that view controller onto the navigation stack
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);

Swift 2.0 及更早版本

// Access the storyboard and fetch an instance of the view controller
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var viewController: MainViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as MainViewController

// Then push that view controller onto the navigation stack
var rootViewController = self.window!.rootViewController as UINavigationController
rootViewController.pushViewController(viewController, animated: true)

关于ios - 从 App Delegate swift 执行 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27135997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com