【问题标题】:Xcode storyboard switchXcode 故事板开关
【发布时间】:2012-01-22 00:18:56
【问题描述】:

有人帮我理解 Xcode 4.2 中的新故事板吗?
我知道如何编写代码来加载另一个视图控制器,但在情节提要模式下存在差异..

我也知道有很多关于导航控制器的教程,但我只是想在storyboard上切换UIViewControllers。

使用普通的 .xib 文件,我可以使用 RootViewController 中的此代码切换视图..

SecondViewController *Second = [[SecondViewController alloc] initWithNibName:nil bundle:nil]; [self presentModalViewController:第二个动画:YES];

当我在情节提要模式下使用它时,它只是在 SecondViewController.m 上加载 UIAlertView 并且屏幕看起来是黑色的?

任何帮助将不胜感激,同时附上 Xcode 项目...

Here is the zip..

-x- 杰伊鲁本

【问题讨论】:

    标签: objective-c xcode storyboard viewcontroller


    【解决方案1】:

    你可以这样做:

    SecondViewController *second= [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
    [self presentModalViewController:second animated:YES];
    

    不要忘记给第二个视图控制器一个标识符,比如“second”。

    否则,您可以使用 segue 连接两个视图控制器。按住 CTRL 从第一个视图控制器拖动到第二个视图控制器。现在您可以选择“push”并为 segue 命名以编程方式切换视图,如下所示:

    [self performSegueWithIdentifier:@"second" sender:self];
    

    只有在设置了导航控制器的情况下,Push Segues 才会起作用。

    【讨论】:

      【解决方案2】:

      你也可以这样切换:

      // get the view that's currently showing
      UIView *currentView = self.view;
      // get the the underlying UIWindow, or the view containing the current view
      UIView *theWindow = [currentView superview];
      
      UIView *newView = aTwoViewController.view; 
      
      // remove the current view and replace with myView1
      [currentView removeFromSuperview];
      [theWindow addSubview:newView];
      
      // set up an animation for the transition between the views
      CATransition *animation = [CATransition animation];
      [animation setDuration:0.5];
      [animation setType:kCATransitionPush];
      [animation setSubtype:kCATransitionFromRight];
      [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
      
      [[theWindow layer] addAnimation:animation forKey:@"SwitchToView2"];
      

      下载sample project here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-18
        • 2015-09-16
        • 2012-01-13
        相关资源
        最近更新 更多