xcode 以编程方式推送模态视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11621754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:00:20  来源:igfitidea点击:

Programmatically push modal view controller

objective-cxcodecocoa-touchuiviewcontrollermodal-dialog

提问by Julian Coltea

Possible Duplicate:
How to create custom modal segue in 4.2 Xcode using storyboard

可能的重复:
如何使用故事板在 4.2 Xcode 中创建自定义模态转场

So I have a viewController that I want to push onto my current view controller modally. Here is the screenshot of my storyboard, with the view I'm trying to load highlighted: enter image description here

所以我有一个 viewController,我想以模态的方式推送到我当前的视图控制器上。这是我的故事板的屏幕截图,其中突出显示了我正在尝试加载的视图: 在此处输入图片说明

So I've created a segue between the current view and the second navigation controller, lets call it "alert". Because I don't have the segue tied to any sort of button, I'm just trying to load the view modally within the following if statement:

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

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

How do I go about this? Do I need to implement some sort of delegate or the prepareForSegue method?

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

回答by matsr

You could use a segue, but here's another alternative:

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

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

Remember to set the identifier of the view controller in your storyboard.

请记住在故事板中设置视图控制器的标识符。

More information and an example project here.

更多信息和示例项目在这里

回答by ryudice

presentModalViewControlleris code for iOS 6 but I need code for iOS 9. Can anyone help?

presentModalViewController是 iOS 6 的代码,但我需要 iOS 9 的代码。有人可以帮忙吗?

回答by Aleksander Azizi

The answer's on How to create custom modal segue in 4.2 Xcode using storyboardshould help.

关于如何使用故事板在 4.2 Xcode 中创建自定义模态转场的答案应该会有所帮助。

T.J's answer:

TJ的回答:

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's Answer:

tiguero的回答:

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

Any of those answer's should answer this question as well.

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

回答by Aleksander Azizi

In the storyboard, create a segue from the Demo Video Capture View Controller to your new view controller by ctrl-dragging from the view controller itself to the new view controller. You do this by ctrl-dragging from the yellow button in the black bar below the Demo Video Capture View Controller to the new view. When it asks which kind select modal. Still in storyboard, click on the segue and identify it as "alert" in the identity navigator on the right. Now go to your .m file and use this implementation...

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

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