0

if i have two viewControllers and i want when condition succeeded i want to go to another ViewController.

but i want to check every time the app launched so i stored the value on userDefaults and in appDelegate i checked for this value.

when i print the value i get it but i can not go to the desired controller.

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
}

i print the value in other controller and it return me true(my value) so why i can not go to another controller

mEldewaik
  • 55
  • 1
  • 8
  • rootViewController doesn't have navigationController probably. try to present it modally – mkowal87 Apr 16 '18 at 11:58
  • Because you have push to VC not pop and add else condition so that if active is false then you can send user to another screen – dahiya_boy Apr 16 '18 at 12:07

4 Answers4

1

You can try this:

In appDelegate and in didFinishLaunching

You can store this value in UserDefault and then check the condition:

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()
    }
Abhirajsinh Thakore
  • 1,608
  • 1
  • 9
  • 21
0

Add a segue from Navigation controller to desired View Controller (with segue identifier "finalBookingVC") and replace the code inside your if condition with:

self.window?.rootViewController!.performSegue(withIdentifier: "finalBookingVC", sender: nil)
S.S.D
  • 1,255
  • 1
  • 9
  • 21
0

You can initiate your view controller in AppDelegate as below. Perform your condition check and set the StoryboardID and ViewControllerID as per your requirement.

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
}
SWAT
  • 957
  • 7
  • 17
0

The first thing you need to check if an application is active and already open (top controller is finalBookingVC) then no need to do anything,

second thing finalBookingVC if already available in a stack then no need to push at that time you need to pop view controller.

If finalBookingVC is not available on the stack then you need to push this controller.

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);
                }
            }
    }
AtulParmar
  • 3,835
  • 15
  • 41