2017-04-26 61 views
0

我一直在研究這個問題一段時間,並認爲我會尋求一些幫助。我有3個視圖控制器:1個導航控制器,1個主控制器和1個詳細視圖控制器。從子視圖導航視圖控制器

在主視圖控制器中,我有一系列按鈕的子視圖。但是,由於類結構,我無法直接調用self.storyboard來獲取當前故事板對象。

我已經嘗試了2種不同的方法,各種方式,但仍然不成功。我在下面發佈了我的方法,並描述了每個細分市場中沒有發生的事情和情況。總體目標是通過點擊子視圖中的按鈕來呈現子視圖控制器(詳細視圖),該子視圖中無法直接訪問父級故事板。

方法1

//Instantiate the new view controller 
ProfileViewViewController *tempViewToShow = [del.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"profile"]; 

// Pass data into the new view controller 
tempViewToShow.thisUser = self.postUser; 

// Output a simple log to ensure both were created 
NSLog(@"Temp User Name: %@, Profile Desc: %@", [tempViewToShow.thisUser getFullName], tempViewToShow.description); 

// Using the AppDelegate for the RootViewController, present the detail view 
[UIApplication.sharedApplication.delegate.window.rootViewController presentViewController:tempViewToShow animated:YES completion:NULL]; 

問題 這一系列的問題是,詳細視圖攜帶導航控制器(因爲它未提及),然而,這種方式仍然顯示完整的視圖控制器

方法2

...

// Use the Delegate and the navigation controller to present the new view controller 
[UIApplication.sharedApplication.delegate.window.rootViewController.navigationController presentViewController:tempViewToShow animated:YES completion:NULL]; 

問題 不顯示任何內容

方法3

// Use the recommended 'pushViewController' for the navigation controller to carry over 
[UIApplication.sharedApplication.delegate.window.rootViewController.navigationController pushViewController:tempViewToShow animated:NO]; 

問題 不顯示任何內容


恩託託,我會如何做這項工作?我會修改哪些行,以及如何修改?謝謝!

+0

你想要做什麼?你想要導航欄嗎? – KKRocks

回答

0

就可以解決這個問題是這樣的:

ProfileViewViewController *tempViewToShow = [del.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"profile"]; 
UINavigationController *naviController = [[UINavigationController alloc] tempViewToShow]; 

然後做到這一點:

[UIApplication.sharedApplication.delegate.window.rootViewController presentViewController:naviController animated:YES completion:NULL]; 
+0

感謝您的及時回覆;但是,這不起作用,因爲NavigationController位於rootViewController(或顯示的當前位置),而不是DetailViewController。 – Jstngoulet

0

您可以從故事板分鏡腳本名,一旦實例你有正確的故事板的實例,讓NavigationController來自其標識符,以及來自其標識符的detailviewController。在Navigationviewcontroller上推送detailviewcontroller。

GET storyboard--在 「MainSToryboard」 你的故事板

UIStoryboard *storyboard = 
[UIStoryboard storyboardWithName:@"MainStoryboard" 
          bundle:[NSBundle mainBundle]]; 

得到Navigationcontroller的實例的替代名稱 - 更換標識:

的UINavigationController * navController =(UINavigationController的*) [故事板instantiateViewControllerWithIdentifier:@ 「navcontroller」];

GET detailviewconrtoller:當前navigationcontroller

UIViewController *detailvc= 
[storyboard instantiateViewControllerWithIdentifier:@"profile"]; 

推細節:

[navController pushViewController:detailvc animated:YES]; 
+0

這產生了我以前遇到的同樣的問題。不顯示任何東西。 – Jstngoulet

0

我發現了一個替代的解決方案。的原因是因爲不正確的視圖控制器正在由

UIApplication.sharedApplication.delegate.window.rootViewController.* 

解決方法是稱爲:

在主視圖控制器類,我通過顯示的ViewController入委託類。然後,從我想調用的子類中,我引用了該視圖控制器和導航控制器,它工作得很好。我的最終代碼如下:

// Create the detail View Controller 
ProfileViewViewController *tempViewToShow = [del.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"profile"]; 

// Set the user variable in the detail view controller 
tempViewToShow.thisUser = self.postUser; 

// Push the view controller into the navigation controller 
// Note that del.currentNav comes from this code: 
/* 
* In this class, create the delegate reference 
* AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate] 
* 
* In the Delegate class, get the set the current navigation controller {let currentVC : UIViewController = passedInVC} 
* self.currentNav = currentVC.navigationController; 
*/ 
[del.currentNav pushViewController:tempViewToShow animated:YES];