2011-11-02 58 views
28

可能有一个简单的解决方案,但我无法弄清楚。链接一个新的viewcontroller到故事板?

我正在使用界面的故事板。

我从一个标签栏控制器开始,但在允许用户使用该应用程序之前,用户必须通过在开始时通过模态推送的loginview来验证自己。

我想在同一个故事板上配置loginview,但我无法缝制出如何链接视图控制器在故事板和我的代码。

我做了什么:

  • 创建一个新的UIViewController子类低谷文件>新建>新建文件。
  • 将在故事板
  • 设置类的自定义类标签
  • 一个新的UIViewController拖动一个UILabel测试的目的。
  • 运行

无标签......在一个新的UIViewController将充当登录视图控制器到MainStoryboard

回答

40

拉。在属性检查器中将标识符更改为LoginViewController(或其他适当的)。然后添加

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"]; 
    [vc setModalPresentationStyle:UIModalPresentationFullScreen]; 

    [self presentModalViewController:vc animated:YES]; 
} 

到第一个视图控制器和登录屏幕将从故事板加载并呈现。

希望这会有所帮助。

+0

不,那不是我的意思。我有.m和.h文件,我不需要.xib文件,但在主要故事板中配置视图。 – Justin

+0

我已经解释了如何在主Storyboard内创建登录屏幕,所以你不需要一个xib。 –

+0

但这不是我想要的。例如,当您有一个动态表视图时,您想在按下单元格时推送视图。但你怎么能用故事板做到这一点。 – Justin

6

上面的Scott Sherwood的回答是我在搜索后发现的最正确答案。尽管按照新的SDK(6.1)进行了非常轻微的更改,但presentModalViewController显示已弃用。

这是对上述答案的非常小的改变。

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil]; 
    HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:@"LoginView"]; 
    [hvc setModalPresentationStyle:UIModalPresentationFullScreen]; 
    [self presentViewController:hvc animated:YES completion:nil]; 
+0

感谢您的分享! – Justin

1

我是这个领域的新人。但是,如果第一个视图控制器是一个导航视图控制器,并且它的rootviewcontroller是一个表视图控制器。如果在单击单元格时想要像LoginViewController一样推送视图控制器,并且您还想使用导航栏返回到表视图。我推荐这种方式:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    UIViewController *controller = [sb instantiateViewControllerWithIdentifier:@"LoginViewController"]; 
    [self.navigationController pushViewController:controller animated:YES]; 
} 

这样,你可以有导航。

顺便说一句,我不知道为什么你问这个问题会出现。我猜想在代码中创建loginviewcontroller时,它的视图不是故事板中的视图。如果有人知道原因,请告诉我!谢谢!

相关问题