如果我有两个viewControllers,我希望当条件成功时,我想转到另一个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
}
我在其他控制器中打印该值并返回true(我的值)所以为什么我不能去另一个控制器
答案 0 :(得分:1)
你可以试试这个:
在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()
}
答案 1 :(得分:0)
将一个segue从导航控制器添加到所需的View Controller(带有segue标识符“finalBookingVC”),并将if条件中的代码替换为:
self.window?.rootViewController!.performSegue(withIdentifier: "finalBookingVC", sender: nil)
答案 2 :(得分:0)
您可以在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
}
答案 3 :(得分:0)
首先需要检查应用程序是否处于活动状态并且已经打开(顶部控制器是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);
}
}
}