无法从xib调用故事板

3

我是iOS开发的新手。我正在尝试从xib文件连接到storyboard。我的xib视图控制器有一个“登录”,成功后应该连接到storyboard。我已经在Google和StackOverflow上搜索了解决方案,我正在使用到处都提供的这段代码:

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

我已经为我的Storyboard创建了一个标识符。不过,无论我尝试什么方法都无法重定向到Storyboard。当登录完成后,我回到我的主视图控制器(xib),而应该重定向到Storyboard。下面是我在名为ProfileTabView.m的文件中的代码:

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

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

}

我已经在登录成功后被调用的函数中实现了这段代码。然而,“Storyboard”故事板从未被调用过。我是否做得对?我应该在其他地方写这段代码吗?
非常感谢任何帮助 :)

1
你需要通过push/present/addSubview的方式将"yourView".view添加到视图层次结构中。这段代码在loginViewDidFinish中的哪个部分? - San
2个回答

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在导航控制器中,那么您将推送新的视图控制器:
[self.navigationController pushViewController:yourView animated:YES]; 

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)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接