Search code examples
iosswiftuiviewcontrolleruistoryboardsegueappdelegate

Can't Call Segue By Identifier from AppDelegate


So I have a segue from ViewController to SecondViewController. This segue is triggered by a UIButton in ViewController and the modally presents SecondViewController. The Segue's Identifier is set ("SegueIdentifier") and I am able to call the segue programatically from within my ViewController.

When I try to do the same in my AppDelegate, I get the error that the compiler can't find a segue with the Identifier I set.

let viewController = ViewController()            
viewController.performSegueWithIdentifier("SegueIdentifier", sender: nil)

Again, I literally copied and pasted the performSegueWithIdentifier method call from the aforementioned method in ViewController in which I also call performSegueWithIdentifier for the same segue and it works.

Any ideas?


Solution

  • In my one project I am managing this situation like,

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    NSString *identifier;
    BOOL isSaved = [[NSUserDefaults standardUserDefaults] boolForKey:@"loginSaved"];
    if (isSaved)
    {
        identifier=@"home1";
    
    
    
    }
    else
    {
        identifier=@"dis1";
    }
    UIStoryboard *    storyboardobj=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *screen = [storyboardobj instantiateViewControllerWithIdentifier:identifier];
    
    
    
    [self.window setRootViewController:screen];
    
    
    return YES;
    }
    

    It is in objective c and just for reference. If it can help you :)

    Update :

    In swift something like,

     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
    
    
        var identifier: String = String()
    
        let isSaved = NSUserDefaults.standardUserDefaults().boolForKey("loginsaved")
    
    
        if isSaved
    
        {
            identifier = "home1"
        }
        else{
    
            identifier = "dis1"
        }
    
        let storyboardobj: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let screen: UIViewController = storyboardobj.instantiateViewControllerWithIdentifier(identifier)
        self.window!.rootViewController = screen
    
        // Override point for customization after application launch.
        return true
    }
    

    Hope this will help :)