2012-01-12 51 views
4

我得到了dismissModalViewControllerAnimated对以下设置正常工作,但很困惑,为什么它适用于自我(在modalViewController),而不是parentViewController。dismissModalViewControllerAnimated工作自我但不parentViewController

这里的设置:

  1. 我有一个UITableViewController用导航按钮调用了一个模式的看法:


    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     self.title = @"Root"; 

     _data = [NSArray arrayWithObjects:@"One", @"Two", nil]; 
     _detailController = [[DetailViewController alloc] init]; 

     // Uncomment the following line to preserve selection between presentations. 
     // self.clearsSelectionOnViewWillAppear = NO; 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem; 

     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showAbout)]; 
    } 

    - (void)showAbout 
    { 
     AboutViewController *abv = [[AboutViewController alloc] init]; 
     abv.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
     [self presentModalViewController:abv animated:YES]; 
    } 

这里的模态视图控制器AboutViewController与工具栏按钮触发关闭模式的解雇动作:



    - (IBAction)dismissAction:(id)sender { 
     [self dismissModalViewControllerAnimated:YES]; 
    } 

我的问题是为什么[自dismissModalViewControllerAnimated]工作,而不是[self.parentViewController dismissModalViewControllerAnimated]?这是iOS 5中的新功能吗?我认为只有parentViewController是可以解雇儿童模态视图?

谢谢!

回答

10

[self dismissModalViewControllerAnimated:YES];曾为相当长的一段时间。如果你问我,就是iOS开发中保存最好的秘密之一。

但是self.parentViewController不工作实际上是iOS 5的新功能。它已被self.presentingViewController“替换”。

这会导致代码试图将前的iOS 5兼容的一个有趣的问题。既然你已经找到self.parentViewController返回nil在iOS 5和UIViewControllers不给presentingViewController前的iOS 5

它留给我们做这样的回应:

if ([self respondsToSelector:@selector(presentingViewController)]){ 
    [self.presentingViewController dismissModalViewControllerAnimated:YES]; 
} 
else { 
    [self.parentViewController dismissModalViewControllerAnimated:YES]; 
} 
+0

由于这是有道理的,现在是越来越糊涂了。 :) – LeoAlmighty 2012-01-13 06:58:42

+0

哇感谢为什么苹果要做到这一点... – minovsky 2012-08-29 13:07:12

+0

'[self.presentingViewController dismissModalViewControllerAnimated:YES]!'被解雇,我目前看来我有3个级别的模式,并希望摆脱的2他们但这是带我回第二级任何想法 – 2012-09-05 20:27:51

8

而不是使用什么NJones说

:,我会建议坚持

[self dismissModalViewControllerAnimated:YES] 

的原因,这将为所有操作系统的工作文档本身的陈述

“呈现视图控制器负责解除其呈现的视图控制器。 但是,如果您在呈现的视图控制器本身上调用此方法,它会自动将消息转发给呈现视图控制器。

注:关于iOS5.0这种方法虽然dismissModalViewControllerAnimated笔记:方法已不dismissViewControllerAnimated:completion:应该在这里代替

相关问题