2012-03-21 85 views
1

我有兩個編程創建的按鈕,您可以在我的viewDidLoad方法中看到。在模態窗口中,我有一個按鈕,通過委託調用cancelSearch方法。當我在我的cancelSearch方法上放置一個斷點時,它被擊中,所以我知道我的委託設置正確,但即使它調用這一行[self dismissViewControllerAnimated:YES completion:nil];它實際上並沒有關閉模態窗口。Modal窗口不被解僱

以下代碼全部來自我的主控制器視圖。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showActionMenu:)]; 
    actionButton.style = UIBarButtonItemStyleBordered; 

    UIBarButtonItem *searchButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showSearchMenu:)]; 
    searchButtonItem.style = UIBarButtonItemStyleBordered; 

    UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)]; 
    NSArray* buttons = [NSArray arrayWithObjects:actionButton, searchButtonItem, nil]; 
    [toolbar setItems:buttons animated:NO]; 
    self.navigationItem.title = @"Census Management"; 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar]; 


    [[RKClient sharedClient] get:@"censusmanagement" delegate:self]; 
} 

- (IBAction)showActionMenu:(id)sender 
{ 
    [self performSegueWithIdentifier: @"CMActionSegue" sender: self]; 
} 

- (IBAction)showSearchMenu:(id)sender 
{ 
    ehrxCMSearchView *search = [[self storyboard] instantiateViewControllerWithIdentifier:@"cmSearch"]; 
    search.selectedOptions = self.selectedOptions; 

    search.delegate = self; 

    [self.navigationController pushViewController:search animated:YES]; 
} 

- (void)cancelSearch:(ehrxCMSearchView *)controller 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

回答

4

你會使用類似的東西駁回模式視圖

[self presentModalViewController:search animated:YES]; 

然而:其使用類似的東西裝入的模態視圖看您的代碼段,它出現在搜索視圖控制器正在使用下面的行推送到導航堆棧:

[self.navigationController pushViewController:search animated:YES]; 

所以我你可能需要跳出從導航堆棧的看法,而不是試圖將其關閉作爲模式視圖:

[self.navigationController popViewControllerAnimated:YES]; 
+0

我認爲你是對的。我改變了它,因爲我傳遞了它的數據。所以我這樣做的方式,我想這甚至是一個模態的觀點了。由於我有我的委託設置,我可以從主視圖控制器嗎?那裏的最後一行看起來像我會從我展示的新視圖中執行。 – Jhorra 2012-03-21 19:17:42

+0

關於它的更多思考,我猜是因爲我推動視圖而不是將其視爲模式,我應該能夠將數據傳回主控制器,然後手動從後者調用返回功能。 – Jhorra 2012-03-21 19:21:06

+0

@Jhorra您可以從主視圖控制器或新視圖控制器調用-popViewControllerAnimated:因爲它們(理論上)應該使用相同的UINavigationController。對於模態視圖,您需要從呈現模態視圖控制器(很可能是您的主視圖控制器)的視圖中調用-dismissModalViewControllerAnimated。 – 2012-03-21 19:49:51

0

如果您的視圖控制器模態呈現,你應該這樣做:

[self.presentingViewController dismissModalViewControllerAnimated:YES]; 

的presentingViewController屬性是iOS 5中唯一可用的。所以,如果你的目標是老版本的iOS,你必須改用self.parentViewController(對每個iOS版本使用適當的版本,你必須處理這個)。

如果你能使你的父母這種控制/展示視圖控制器,然後就稱之爲:

[self dismissModalViewControllerAnimated:YES]; 

這將解僱:

[self dismissModalViewControllerAnimated:YES]; 
+0

從別人告訴我使用委託是首選的方法,所以我試圖這樣做。這也有幫助,因爲我想將模態控制器的數據傳回主控制器。 – Jhorra 2012-03-21 19:08:50

+0

這兩種方法都在iOS 6.0中折舊。 – Zorayr 2014-03-23 16:50:26