我一直在研究这个问题,并且认为我会寻求一些帮助。我有3个视图控制器:1个导航控制器,1个主控制器和1个详细视图控制器。
在主视图控制器中,我有一系列带按钮的子视图。但是,由于类结构,我无法直接调用self.storyboard来获取当前的storyboard对象。
我尝试了两种不同的方法,多种方式,但仍然不成功。我在下面发布了我的方法,并描述了每个细分中的内容和内容。总体目标是通过点击子视图中的按钮来呈现子视图控制器(详细视图),该子视图无法直接访问父故事板。
方法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];
问题的 这个系列的问题是详细视图不携带导航控制器(因为没有提到),但是,这种方式仍然显示完整的View Controller
方法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 :(得分: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];
答案 1 :(得分:0)
您可以从故事板名称创建故事板实例。您可以使用正确的故事板实例,从其标识符获取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"];
在当前navigationcontroller上推送详细信息:
[navController pushViewController:detailvc animated:YES];
答案 2 :(得分: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];