2017-04-26 123 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];