Call a view controller programatically using storyboard id iOS

不羁的心 提交于 2019-12-31 03:15:09

问题


What I Want?

  • In MainViewController I open a view from a xib programmatically. This xib view contains a UIButton. The xib opens successfully.
  • On click of that UIButton I want to move FeedBackViewController

My code

  • This is the code I am using to move FeedBackViewController on click of the UIButton

    FeedBackViewController *view=[self.storyboad instantiateViewControllerWithIdentifier:@"FeedBackView"];
    [self presentViewController:view animated:YES completion:nil];
    

But it is crashing and I am receiving the following message:

Terminating app due to uncaught exception 'NSInvalidARgumentException', reason: 'Application tried to present a nil modal view controller on target FutureOrderViewController: 0x199a4a30.


回答1:


Check these things

Check the identifier of the viewcontroller, if it is the same that you mentioned in storyboard

Make sure that your view object is not nil. Set a breakpoint on that line and on the debug area at the bottom see whether the object is not nil. If it is nil then problem lies in the storyboard connection

If your current viewcontroller is not launched from storyboard then get storyboard object like this :

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController * feedbackVC = [storyboard   instantiateViewControllerWithIdentifier:@"FeedBackView"] ;
[self presentViewController:feedbackVC animated:YES completion:nil];



回答2:


whenever you want to load a VC as the way you mentioned, you need two things.

Thing 1: Specific storyboard ID. you will find this by:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Your_Storyboard_Id" bundle:nil];

In my case this is Main

Thing 2:

You need your View Controller id. Just click on the View Controller you want to go from storyboard and give a suitableName there.

Then invoke this like:

UIViewController * yourVC = [storyboard   
instantiateViewControllerWithIdentifier:@"YourVC_ID"] ;

And if you want to present it modally you already did this. :)

[self presentViewController:yourVC animated:YES completion:nil];



回答3:


Try this

UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
FeedBackViewController * feedbackVC = [story   instantiateViewControllerWithIdentifier:@"FeedBackView"] ;
[self presentViewController:feedbackVC animated:YES completion:nil];

Where FeedBackView is your storyboard id of FeedBackViewController.



来源:https://stackoverflow.com/questions/44022707/call-a-view-controller-programatically-using-storyboard-id-ios

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