How to perform Segue in AppDelegate?

13,820

Solution 1

If you consider pushing view manually rather then segueperform following code most probably will work for you

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if ([[DBSession sharedSession] handleOpenURL:url]) {
        if ([[DBSession sharedSession] isLinked]) {

            NSLog(@"App linked successfully!");
            // At this point you can start making API calls

            //push view manually 
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
            LoginDropboxViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"LoginDropbox"];
            [(UINavigationController*)self.window.rootViewController pushViewController:ivc animated:NO];



    }
        return YES;
    }
    // Add whatever other url handling code your app requires here
    return NO;
}

Solution 2

You can do it like this:

UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;

[[[navigationController viewControllers] objectAtIndex:0] performSegueWithIdentifier:@"goToMeeting" sender:self];

This will only work if the index in viewControllers array matches the one of your view controller and if it exists of course. In this case is the first one (in the array and storyboard).

The segue ("goToMeeting") must not be attached to an action. The way you do this is by control-dragging from the file owner icon at the bottom of the storyboard scene to the destination scene. A popup will appear that will ask for an option in “Manual Segue”; pick “Push” as the type. Tap on the little square and make sure you’re in the Attributes Inspector. Give it an identifier which you will use to refer to it in code.

Share:
13,820
SpaceDust__
Author by

SpaceDust__

#SOreadytohelp

Updated on June 05, 2022

Comments

  • SpaceDust__
    SpaceDust__ about 2 years

    I am trying to complete an application on IOS 5.1 with Storyboard. Basically I am doing a dropbox app. Since I am using Dropbox SDK link to Dropbox is handled in AppDelegate.m. User has the option of be able to unlink from a session and link again in different View Controllers. So every time user link and unlinked app has to switch view from Appdelegate to a view controller that is unconnected to rootviewcontroller

    In original Dropbox's example Dropbox handled transition like following code

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
        if ([[DBSession sharedSession] handleOpenURL:url]) {
            if ([[DBSession sharedSession] isLinked]) {
                [navigationController pushViewController:rootViewController.photoViewController animated:YES];
            }
            return YES;
        }
    
        return NO;
    }
    

    But I am using Storyboard with Navigation Controller and any of the following methods are not working I put methods in comments.

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
        if ([[DBSession sharedSession] handleOpenURL:url]) {
            if ([[DBSession sharedSession] isLinked]) {
    
                NSLog(@"App linked successfully!");
                // At this point you can start making API calls
    
                /*UIViewController *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"MeetingViewController"];
                [self.navigationController pushViewController:viewController animated:YES]; */
    
               //[self performSegueWithIdentifier:@"xxxx" sender:self];
    
               /* LoginDropboxViewController *loginController=[[LoginDropboxViewController alloc] initWithNibName:@"LoginDropbox" bundle:nil];
                [navigationController pushViewController:loginController animated:YES]; */
    
            }
            return YES;
        }
        // Add whatever other url handling code your app requires here
        return NO;
    }
    

    Here is the storyboard of the app enter image description here

    So how can I switch view in AppDelegate.h ?

    Note: If I add a segue and name the segue lets say goToMeeting [self performSegueWithIdentifier:@"goToMeeting" sender:self];

    error I get is : No Visible @interface for 'AppDelegate' declares the selector performSegueWithIdentifier:sender

  • ChaosSpeeder
    ChaosSpeeder over 9 years
    But what can I do if I need to pass parameter data to the performSegueWithIdentifier call. In app delegate is no ViewController so I couldn't overwrite the prepareForSegue() call?