【问题标题】:Programmatically push modal view controller [duplicate]以编程方式推送模态视图控制器[重复]
【发布时间】:2012-07-22 05:35:53
【问题描述】:

可能重复:
How to create custom modal segue in 4.2 Xcode using storyboard

所以我有一个 viewController 我想以模态方式推送到我当前的视图控制器上。这是我的故事板的屏幕截图,我试图加载的视图突出显示:

所以我在当前视图和第二个导航控制器之间创建了一个 segue,我们称之为“警报”。因为我没有将 segue 绑定到任何类型的按钮,所以我只是尝试在以下 if 语句中以模态方式加载视图:

if([detector judgeSpeed:[ratio floatValue]])
{
    //push the new view here
}

我该怎么做?我是否需要实现某种委托或 prepareForSegue 方法?

【问题讨论】:

    标签: objective-c xcode cocoa-touch uiviewcontroller modal-dialog


    【解决方案1】:

    您可以使用 segue,但这里有另一种选择:

    SomeViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"SomeIdentifier"];
    [self.navigationController presentModalViewController:controller animated:YES]
    

    记得在情节提要中设置视图控制器的标识符。

    更多信息和示例项目here

    【讨论】:

    • 在 iOS 6 之后,这已被弃用。这是建议解决方案的更新版本。 [self presentViewController:second animated:YES completion:NULL];
    【解决方案2】:

    presentModalViewController 是 iOS 6 的代码,但我需要 iOS 9 的代码。谁能帮忙?

    【讨论】:

      【解决方案3】:

      How to create custom modal segue in 4.2 Xcode using storyboard 上的答案应该会有所帮助。

      T.J的回答:

      UIViewController *src = (UIViewController *) self.sourceViewController;
      [UIView transitionWithView:src.navigationController.view duration:0.5
                         options:UIViewAnimationOptionTransitionFlipFromTop
                      animations:^{
                          [src.navigationController popToViewController:[src.navigationController.viewControllers objectAtIndex:0] animated:NO];;
                      }
                      completion:NULL];
      

      tiguero的回答:

      [self.sourceViewController presentModalViewController:self.destinationViewController animated:NO];
      

      这些答案中的任何一个都应该回答这个问题。

      【讨论】:

        【解决方案4】:

        在情节提要中,通过从视图控制器本身 ctrl 拖动到新视图控制器,创建从演示视频捕获视图控制器到新视图控制器的 segue。您可以通过 ctrl 从 Demo Video Capture View Controller 下方黑色条中的黄色按钮拖动到新视图来执行此操作。当它询问哪种类型时选择modal。仍在情节提要中,单击 segue 并在右侧的身份导航器中将其标识为“警报”。现在转到您的 .m 文件并使用此实现...

        if([detector judgeSpeed:[ratio floatValue]])
        {
           [self performSegueWithIdentifier:@"alert" sender:self];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-20
          • 2020-05-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多