這是一個老帖子,但我發現它有用的幫我想以不同的方式,這是我如何解決問題。
我以編程方式創建了我的splitViewController
。然後我用一個數字標記它,並將其作爲子視圖添加到當前視圖中。
FirstViewController* firstView = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
SecondViewController* secondView = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
UISplitViewController* splitVC = [[UISplitViewController alloc] init];
[splitVC setDelegate:secondView];
splitVC.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
splitVC.view.tag = 99;
[self.view addSubview:splitVC.view];
之後,顯示splitView
,但要擺脫它,我必須從視圖中刪除它,所以我創建了viewcontrollers
之間的通知。在主視圖控制器中,我添加了觀察者。 (注:主視圖控制器不是splitViewController
或它的景色之一,它是加載splitViewController
視圖控制器)中選擇「removeSplitView
」我把我所有的當前視圖的子視圖的
NSNotificationCenter *splitViewObserver = [NSNotificationCenter defaultCenter];
[splitViewObserver addObserver:self selector:@selector(removeSplitView) name:@"removeSplitView" object:nil];
通過for循環並使用標籤99搜索UIView類對象並將其從超級視圖中移除。
NSArray *subviews = [self.view subviews];
for (int i = 0; i < [subviews count]; i++) {
if ([[subviews objectAtIndex:i] isKindOfClass:[UIView class]]) {
UIView *tempView = [subviews objectAtIndex:i];
if (tempView.tag == 99) {
[[subviews objectAtIndex:i] removeFromSuperview];
}
}
}
在的firstView我稱做了方法的帖子,主要是ViewController
觀察通知。
-(IBAction) done:(id)sender {
[fileSelectedNotification postNotificationName:@"removeSplitView" object:self];
}
您還必須在應用程序的某處創建fileSelectedNotification
。我通過viewDidLoad
做到了這一點。它看起來像這樣。
fileSelectedNotification = [NSNotificationCenter defaultCenter];
當然我也添加了這個
NSNotiicationCenter *filesSelectedNotification;
這個viewController
的.h文件。
因此,當我按下完成按鈕(這是我的應用程序欄按鈕)時,它將從視圖中刪除splitViewController
。
工作正常。我從閱讀文檔中獲得了所有這些。
您將得到一個未捕獲的異常:「應用程序嘗試以模態方式呈現分割視圖控制器」[原文] – 2010-04-17 16:32:02