簡體   English   中英

子視圖中的導航視圖控制器

[英]Navigation View Controller from Subview

我已經在這個問題上工作了一段時間,並認為我會尋求幫助。 我有3個視圖控制器:1個導航控制器,1個主控制器和1個詳細視圖控制器。

在主視圖控制器中,我有一系列帶有按鈕的子視圖。 但是,由於類結構的原因,我無法直接調用self.storyboard來獲取當前的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];

問題本系列的問題是局部視圖帶有導航控制器(因為未提及),但是,這種方式仍顯示完整的視圖控制器

方法二

...

 // 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];

問題不顯示任何內容


En toto,我將如何進行這項工作? 我將修改哪些行以及如何修改? 謝謝!

您可以這樣解決此問題:

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

然后這樣做:

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

您可以從情節提要名稱創建情節提要的實例。一旦有了正確的情節提要實例,從其標識符獲取NavigationController,並從其標識符獲取detailviewController。 在Navigationviewcontroller上推送detailviewcontroller。

獲取情節提要-在“ MainSToryboard”中替換您的情節提要的名稱

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

獲取Navigationcontroller的實例-替換標識符:

UINavigationController * navController =(UINavigationController *)[故事板InstantiateViewControllerWithIdentifier:@“ navcontroller”];

獲取詳細信息viewconrtoller:

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

推送有關當前導航控制器的詳細信息:

[navController  pushViewController:detailvc animated:YES];  

我找到了替代解決方案。 原因是因為錯誤的視圖控制器被調用

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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM