How to Navigate UIViewController from UIView

时间秒杀一切 提交于 2019-12-25 09:28:29

问题


Hi I'm New to iOS Development and Please give me a solution to navigate to UIViewController from an UIView: This is the code I have used but it is not working

acceptedJobDetailViewController *acceptedJobDetailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"acceptedJobDetailViewController"];
[self.navigationController pushViewController:acceptedJobDetailViewController animated:YES];

Also i tried this below code to push the viewcontroller but that is also not working, Please help me to sort out this:

[appDelegate.navControl pushViewController:acceptedJobDetailViewController animated:YES];

回答1:


please add navigation controller in your storyboard.

step -1: select acceptedJobDetailViewController from storyboard.
step -2: now go to Editor>EmbedIn>NavigationController
step -3: now below code and run your project


acceptedJobDetailViewController *acceptedJobDetailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"acceptedJobDetailViewController"];



回答2:


If you have already added NavigationController in your storyboard and you are trying to push screen from UIView Class than use below code :

UIViewController *superViewController =  (UIViewController*)[self nextResponder];
UINavigationController *navigationController = superViewController.navigationController;
[navigationController pushViewController:<YOUR_VIEW_CONTROLLER_OBJECT> animated:YES];

if you haven't and any navigation please add it before your ViewController like below

Select ViewController -> Go to Editor Menu -> Embed In -> Navigation Controller

Now Navigation Controller added into your storyboard now do what you are doing previously.

acceptedJobDetailViewController *acceptedJobDetailViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"acceptedJobDetailViewController"];
[self.navigationController pushViewController:acceptedJobDetailViewController animated:YES];

Hope this will helps.




回答3:


Try this

[[(UIViewController *)[self.superview nextResponder] navigationController] pushViewController:"your viewcontroller object" animated:NO];

Updated to swift 3

(superview?.next? as? UIViewController)?.navigationController?.pushViewController("your viewcontroller object", animated: false)


来源:https://stackoverflow.com/questions/41849425/how-to-navigate-uiviewcontroller-from-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!