How to perform Segue in AppDelegate?

2020-07-05 07:35发布

问题:

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

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

回答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;
}


回答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.



回答3:

In my case i need to go to dashboard screen from appdelegate using segue, the code as below in objective c.

if([rememberMe isEqualToString:@"YES"]){
        //Goto Dashboard
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        SWRevealViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"revealVc"];
        self.window.rootViewController = vc;
        [self.window.rootViewController performSegueWithIdentifier:@"sw_front" sender:self];
        [self.window makeKeyAndVisible];
    }else{
        //Goto login
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LoginViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
        self.window.rootViewController = vc;
        [self.window makeKeyAndVisible];
    }

Write inside your didFinishLaunchingWithOptions

Hope it helps some one.