【问题标题】:Navigation View Controller from Subview来自子视图的导航视图控制器
【发布时间】:2017-04-27 09:07:22
【问题描述】:

我一直在研究这个问题一段时间,并认为我会寻求一些帮助。我有 3 个视图控制器:1 个导航控制器、1 个主控制器和 1 个详细视图控制器。

在主视图控制器中,我有一系列带有按钮的子视图。但是,由于类结构的原因,我无法直接调用 self.storyboard 来获取当前的故事板对象。

我尝试了 2 种不同的方法,多种方式,但仍然不成功。我在下面发布了我的方法,并描述了每个部分中发生了什么和没有发生什么。总体目标是通过点击子视图中的按钮来呈现子视图控制器(详细视图),该子视图无法直接访问父情节提要。

方法一

//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,我将如何进行这项工作?我将修改哪些行以及如何修改?谢谢!

【问题讨论】:

  • 你想做什么?你想要导航栏吗?

标签: ios objective-c


【解决方案1】:

你可以这样解决这个问题:

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 位于 rootViewController(或当前显示的),而不是 DetailViewController。
【解决方案2】:

您可以从故事板名称创建故事板实例。一旦您拥有正确的故事板实例,从其标识符获取 NavigationController,并从其标识符获取 detailviewController。在 Navigationviewcontroller 上推送 detailviewcontroller。

获取故事板——在“MainSToryboard”中替换故事板的名称

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

获取 Navigationcontroller 的实例 - 替换标识符:

UINavigationController *navController =(UINavigationController *) [storyboard instantiateViewControllerWithIdentifier:@"navcontroller"];

获取 detailviewconrtoller :

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

在当前导航控制器上推送详细信息:

[navController  pushViewController:detailvc animated:YES];  

【讨论】:

  • 这产生了我以前遇到的相同问题。不显示任何内容。
【解决方案3】:

我找到了替代解决方案。原因是因为

调用了不正确的视图控制器
UIApplication.sharedApplication.delegate.window.rootViewController.*

解决方法是

在主视图控制器类中,我将显示的视图控制器传递给委托类。然后,从我想调用的子类中,我引用了那个视图控制器和导航控制器,它工作得很好。我的最终代码如下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 2012-05-01
    • 2014-04-10
    相关资源
    最近更新 更多