2012-07-29 89 views
0

我的應用程序有一個主屏幕(mainView)。在mainView上,我有一個按鈕。當用戶點擊它時,我使用如何在視圖控制器中關閉視圖控制器中的父視圖?

[self presentViewController:libraryView animated:YES completion:nil] 

以呈現另一個視圖(libraryView)。然後,在libraryView,我用

[self.view addSubview:tabBarController.view] 

添加UITabBarController有2個視圖控制器:featuredBooksrecentBooks

,一切工作正常。但是,當我加入一個按鈕featureBooks到dissmiss的libraryView,並返回到mainView,下面的方法不起作用

[self dismissViewControllerAnimated: YES completion:nil] 
[self.parentViewController dismissViewControllerAnimated: YES completion:nil] 
[self.presentingViewController dismissViewControllerAnimated: YES completion:nil] 

我知道了原因:selffeatureBooks的看法,而不是libraryView

那麼,我如何參考libraryView,關閉它並從Tab Bar Controller中的視圖控制器(featureBooksrecentBooks)返回mainView

非常感謝。

回答

0

製作libraryView的mainView委託對象...然後,當您從libraryView調用委託方法時,mainView將在其代碼中調用dismissViewcController方法。

所以:

1)創建到libraryView控制器的.h代碼:

@protocol LibraryViewDelegate 

     - (void) LibraryViewDelegate_DismissButtonClicked; 

@end 

2)然後創建一個屬性到MAINVIEW h文件:

@property(nonatomic, assign) NSObject<LibraryViewDelegate> *delegate; 

和以下的成.m one

@synthesize delegate; 

並分配到的MainView對象libraryView創建之後和之前這個屬性,你將呈現它

3)編寫以下代碼到MAINVIEW .m文件:

-(void)LibraryViewDelegate_DismissButtonClicked{ 
     //put here the code for dismissing mainView created modalViewController (libraryView) 
} 

4)然後編寫調用的代碼:

[self.delegate LibraryViewDelegate_DismissButtonClicked]; 

到libraryView當你按下按鈕辭退

+0

謝謝您的回答和示例代碼。在詢問示例代碼後,我搜索並刪除了我的評論:P無論如何,謝謝。 但是,我想你可能會誤解我。我想從'featuredBooks'(一個選項卡欄控制器的視圖控制器,並且該選項卡欄控制器是'libraryView'的子視圖)中關閉'libraryView'。我試過你的代碼,但它沒有按我想要的方式工作。 再次感謝您的代碼。 – 2012-07-29 09:25:23

+0

最後,我做到了。我不再使用Interface Builder,並以編程方式創建Tab Bar Controller,View Controller。現在代表工作正常。 – 2012-07-29 10:27:30

相關問題