无法从xib调用storyboard

时间:2013-10-31 03:50:28

标签: ios iphone objective-c uiviewcontroller xcode5

我是iOS开发的新手。我正在尝试从xib文件连接到故事板。我的xib View Controller有一个“Login”,如果成功应该连接到storyboard。我在googleflow上搜索并搜索了一个解决方案,我正在使用这个代码随处可见:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    YourViewController * yourView = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:@"identifier ID"];

我也为我的故事板创建了一个标识符。但是,无论我尝试什么,我都不会被重定向到故事板。登录完成后,我将返回主视图控制器(xib)。我应该被重定向到故事板。以下是我的代码在名为ProfileTabView.m的文件中的样子:

-(void) loginViewDidFinish:(NSNotification*)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    ProfileTabView * yourView = (ProfileTabView *)[storyboard instantiateViewControllerWithIdentifier:@"myID"];

}

我已经在登录成功后调用的函数中实现了此代码。然而,故事板“故事板”永远不会被调用。我这样做对吗?我应该在其他地方写这段代码吗?

非常感谢您的帮助:)

2 个答案:

答案 0 :(得分:3)

又一步:展示新的视图控制器......

-(void) loginViewDidFinish:(NSNotification*)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    ProfileTabView * yourView = (ProfileTabView *)[storyboard instantiateViewControllerWithIdentifier:@"myID"];

    // it might be better to rename 'ProfileTabView' as 'ProfileTabViewController'
    // and 'yourView' as 'profileTabVC', by convention.  Anyway ...

    [self.presentViewController:yourView animated:YES completion:^{}];
}

如果您的登录VC位于导航控制器中,那么您将推送新的vc:

[self.navigationController pushViewController:yourView animated:YES]; 

答案 1 :(得分:1)

-(void) loginViewDidFinish:(NSNotification*)notification
{
NSNotificationCenter.defaultCenter().removeObserver(self)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("ViewControllerID") as UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
}