如何从 appDelegate 到 viewController
How to go from appDelegate to viewController
如果我有两个 viewController,并且我想在条件成功时转到另一个 ViewController。
但我想在每次应用程序启动时检查,所以我将值存储在 userDefaults 和 appDelegate 中,我检查了这个值。
当我打印值时,我得到了它,但我无法转到所需的控制器。
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let active:Bool = defaults.bool(forKey: "activeBooking")
if active {
print("active \(active)")
let rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Final", bundle: nil)
let setViewController = mainStoryboard.instantiateViewController(withIdentifier: "finalBooking") as! BookingFromCurrentLocationVC
rootViewController?.navigationController?.popToViewController(setViewController, animated: false)
}
return true
}
我在其他控制器中打印值,它 return 我是真的(我的值)所以为什么我不能去另一个控制器
你可以试试这个:
在 appDelegate
和 didFinishLaunching
你可以把这个值存入UserDefault
然后检查条件:
if condition == true{
goToVC1()
}else{
goToVC2
}
func goToVC1() {
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let ObjVC1: ViewController = storyboard.instantiateViewController(withIdentifier: "VC1") as! VC1
let navigationController : UINavigationController = UINavigationController(rootViewController: ObjVC1)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
func goToVC2() {
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let ObjVC2: ViewController = storyboard.instantiateViewController(withIdentifier: "VC2") as! VC2
let navigationController : UINavigationController = UINavigationController(rootViewController: ObjVC2)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
将 Navigation Controller 的 segue 添加到所需的 View Controller(使用 segue 标识符 "finalBookingVC")并将 if 条件中的代码替换为:
self.window?.rootViewController!.performSegue(withIdentifier: "finalBookingVC", sender: nil)
您可以在 AppDelegate 中启动您的视图控制器,如下所示。
执行您的条件检查并根据您的要求设置 StoryboardID 和 ViewControllerID。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
//check your condition here and change the Storyboard name and ViewController ID as required
let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVCID") as! HomeController
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
return true
}
首先您需要检查应用程序是否处于活动状态并且已经打开(顶级控制器是 finalBookingVC)然后无需执行任何操作,
第二件事 finalBookingVC 如果堆栈中已经可用,那么此时无需推送,您需要弹出视图控制器。
如果 finalBookingVC 在堆栈上不可用,那么您需要推送此控制器。
func gotoController() {
let navigationController : UINavigationController! = self.window!.rootViewController as! UINavigationController;
let arrViewController = navigationController;
if arrViewController != nil && !(arrViewController?.topViewController is finalBookingVC) {
var finalBookingVCFound:Bool = false;
for aViewController in (arrViewController?.viewControllers)! {
if aViewController is finalBookingVC {
finalBookingVCFound = true;
_ = navigationController?.popToViewController(aViewController, animated: true);
break;
}
}
if !finalBookingVCFound {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil);
let objVC:finalBookingVC = mainStoryboard.instantiateViewController(withIdentifier: "finalBookingVC") as! finalBookingVC;
navigationController?.pushViewController(objVC, animated: true);
}
}
}
如果我有两个 viewController,并且我想在条件成功时转到另一个 ViewController。
但我想在每次应用程序启动时检查,所以我将值存储在 userDefaults 和 appDelegate 中,我检查了这个值。
当我打印值时,我得到了它,但我无法转到所需的控制器。
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let active:Bool = defaults.bool(forKey: "activeBooking")
if active {
print("active \(active)")
let rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Final", bundle: nil)
let setViewController = mainStoryboard.instantiateViewController(withIdentifier: "finalBooking") as! BookingFromCurrentLocationVC
rootViewController?.navigationController?.popToViewController(setViewController, animated: false)
}
return true
}
我在其他控制器中打印值,它 return 我是真的(我的值)所以为什么我不能去另一个控制器
你可以试试这个:
在 appDelegate
和 didFinishLaunching
你可以把这个值存入UserDefault
然后检查条件:
if condition == true{
goToVC1()
}else{
goToVC2
}
func goToVC1() {
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let ObjVC1: ViewController = storyboard.instantiateViewController(withIdentifier: "VC1") as! VC1
let navigationController : UINavigationController = UINavigationController(rootViewController: ObjVC1)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
func goToVC2() {
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let ObjVC2: ViewController = storyboard.instantiateViewController(withIdentifier: "VC2") as! VC2
let navigationController : UINavigationController = UINavigationController(rootViewController: ObjVC2)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
将 Navigation Controller 的 segue 添加到所需的 View Controller(使用 segue 标识符 "finalBookingVC")并将 if 条件中的代码替换为:
self.window?.rootViewController!.performSegue(withIdentifier: "finalBookingVC", sender: nil)
您可以在 AppDelegate 中启动您的视图控制器,如下所示。 执行您的条件检查并根据您的要求设置 StoryboardID 和 ViewControllerID。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
//check your condition here and change the Storyboard name and ViewController ID as required
let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVCID") as! HomeController
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
return true
}
首先您需要检查应用程序是否处于活动状态并且已经打开(顶级控制器是 finalBookingVC)然后无需执行任何操作,
第二件事 finalBookingVC 如果堆栈中已经可用,那么此时无需推送,您需要弹出视图控制器。
如果 finalBookingVC 在堆栈上不可用,那么您需要推送此控制器。
func gotoController() {
let navigationController : UINavigationController! = self.window!.rootViewController as! UINavigationController;
let arrViewController = navigationController;
if arrViewController != nil && !(arrViewController?.topViewController is finalBookingVC) {
var finalBookingVCFound:Bool = false;
for aViewController in (arrViewController?.viewControllers)! {
if aViewController is finalBookingVC {
finalBookingVCFound = true;
_ = navigationController?.popToViewController(aViewController, animated: true);
break;
}
}
if !finalBookingVCFound {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil);
let objVC:finalBookingVC = mainStoryboard.instantiateViewController(withIdentifier: "finalBookingVC") as! finalBookingVC;
navigationController?.pushViewController(objVC, animated: true);
}
}
}