appdelegate如何使用storyboard pushViewController

时间:2013-01-04 08:13:11

标签: ios ios6 storyboard

在我的app委托中,如果保存了用户详细信息,我将用户推送到视图控制器

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  // cached user
    PFUser *currentUser = [PFUser currentUser];
    if (currentUser)
    {
        NSLog(@"current user signing and looking for VC %@", currentUser);
        // A user was cached, so skip straight to the main view

        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
        TaskSelectionViewController*viewController = [[TaskSelectionViewController alloc] initWithUser:currentUser];
        [navigationController pushViewController:viewController animated:YES];

                NSLog(@"VC found");
            } else {
                NSLog(@"VC not found");
    }

TaskSelectionViewController.m

 -(id)initWithUser:(PFUser *)currentUser
{
NSLog(@"current user %@", currentUser);

NSLog(@"task Selection initwithuser");
return self;
 }

推送工作正常,但我得到的是顶部的NavigationController栏和黑屏。

缺少什么?

感谢您的帮助。

2 个答案:

答案 0 :(得分:24)

使用instantiateViewControllerWithIdentifier完成:@“ID”

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
TaskSelectionViewController *controller = (TaskSelectionViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"task"];
[navigationController pushViewController:controller animated:YES];

答案 1 :(得分:0)

在TaskSelectionViewController的-(id)initWithUser:(PFUser *)currentUser中,您应该调用[super init]或类似的东西来正确初始化ViewController。这至少解决了类似的问题。

查看Apple的示例,了解视图控制器初始化方法的参考实现。