在iOS 7中展示和取消一个模态视图

11

我有一个视图控制器,上面有一个按钮。该按钮是“隐私政策”。当点击它时,会跳转到正确的IBAction并创建隐私控制器。

 - IBAction ...
{
    PrivacyPolicyViewController *privacy = [[PrivacyPolicyViewController alloc] init];
    .....
}

我想在iOS 7中创建一个隐私控制器的模态视图,其中有一个UIWebView会向上动画显示,并带有一个用于关闭它的返回按钮。我在网上看到的所有方法都是iOS 6的,似乎已经过时了。

4个回答

47

使用类似以下代码:

// assuming your controller has identifier "privacy" in the Storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PrivacyPolicyViewController *privacy = (PrivacyPolicyViewController*)[storyboard instantiateViewControllerWithIdentifier:@"privacy"];

// present
[self presentViewController:privacy animated:YES completion:nil];

// dismiss
[self dismissViewControllerAnimated:YES completion:nil];

9

[self presentViewController:vc animated:YES completion:nil];已经被弃用

你可以尝试使用

[self presentViewController:viewController animated:YES completion:nil];

它将为您工作。


6
如果您正在使用Storyboard,您也可以使用segue以编程方式呈现模态视图控制器。
1. 在您的故事板中,从起始视图下方的File's owner图标ctrl+拖动到要模态呈现的视图上,然后选择“modal”选项。
2. 单击segue图标,然后在属性检查器中为其命名一个标识符,例如“toNewView”。
3. 在起始视图控制器的.m文件中,使用以下代码执行模态segue:[self performSegueWithIdentifier:@"toNewView" sender:self]; 这是一种很好的方法,因为您不必导入.h文件来实例化第二个控制器对象用于presentViewController方法。
要关闭它,只需使用unwind segue

谢谢!有没有办法从跳转到的 ViewController 中访问发送者? - Ruben Martinez Jr.
1
@RubenMartinezJr。在segue之前,您实际上需要确定您正在segue的位置。使用prepareForSegue方法,在其中使用if ([segue identifier] isEqualToString:@"someView"])根据您正在segue的位置进行条件操作。 - inorganik

0
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
taskQueeDetails *privacy = (taskQueeDetails*)[storyboard instantiateViewControllerWithIdentifier:@"taskQueeDetails"];

// Present the modal
[self presentViewController:privacy animated:YES completion:nil];

使用这段代码并更改字符串 instantiateViewControllerWithIdentifier:@"taskQueeDetails"],它将正常工作。


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