2012-01-12 50 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:應該在這裏代替

相關問題