如何在AppDelegate中执行Segue?

时间:2012-09-06 17:47:33

标签: objective-c ios storyboard dropbox segue

我正在尝试使用Storyboard在IOS 5.1上完成一个应用程序。基本上我正在做一个Dropbox应用程序。由于我使用Dropbox SDK链接到Dropbox是在AppDelegate.m中处理的。用户可以选择在会话中取消链接,并在不同的View Controller中再次链接。因此,每次用户链接和未链接的应用程序必须将视图从Appdelegate切换到未连接到rootviewcontroller的视图控制器

在Dropbox的原始示例中,Dropbox处理了转换,如下面的代码

- (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;
}

但是我正在使用带有导航控制器的Storyboard,并且以下任何一种方法都不起作用我将方法放在评论中。

- (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;
}

以下是该应用的故事板 enter image description here

那么如何在AppDelegate.h中切换视图?

注意:如果我添加一个segue并命名为segue,请说goToMeeting  [self performSegueWithIdentifier:@“goToMeeting”sender:self];

我得到的错误是:No Visible @interface for 'AppDelegate' declares the selector performSegueWithIdentifier:sender

3 个答案:

答案 0 :(得分:13)

如果你考虑手动推送视图而不是segueperform以下代码很可能适合你

- (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;
}

答案 1 :(得分:5)

你可以这样做:

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

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

这仅在viewControllers数组中的索引与视图控制器中的索引匹配且当然存在时才有效。在这种情况下是第一个(在数组和故事板中)。

不得将segue(“goToMeeting”)附加到某个操作。执行此操作的方法是通过控制 - 从故事板场景底部的文件所有者图标拖动到目标场景。将出现一个弹出窗口,询问“手动搜索”中的选项;选择“推”作为类型。点击小方块,确保您在属性检查器中。给它一个标识符,用于在代码中引用它。

答案 2 :(得分:0)

对于我来说,我需要使用segue(目标c中的以下代码)从appdelegate转到仪表板屏幕。

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];
    }

在您的didFinishLaunchingWithOptions

中写

希望对您有所帮助。