Present View Controller modally from xib file - ios

I have a xib file associated to a UIViewController custom class (ZocaloPlayerViewController)
and im trying to present a UIViewController (made in storyboard) modally with flip horizontal
but i am getting an error:
This is how im trying to present the view controller
ShowDetailsArtistAlbumViewController *vc = [[ShowDetailsArtistAlbumViewController alloc] initWithNibName:#"ShowDetailsArtistAlbumViewController" bundle:nil];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:^{
}];
But i am getting this error...
Uncaught exception: Could not load NIB in bundle: 'NSBundle </var/mobile/Containers/Bundle/Application/57C2AD20-51DB-4C43-BB8F-2E8F22CD2E2A/MyMusicApp.app> (loaded)' with name 'ShowDetailsArtistAlbumViewController'
I was trying with
[self.parentViewController presentViewController:vc animated:YES completion:^{
}];
But nothing happens...
EDIT:
I tried this way
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ShowDetailsArtistAlbumViewController *vc = [sb instantiateViewControllerWithIdentifier:#"ShowDetailsArtistAlbumViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:^{
}];
The view controller is presented but im getting a black view but nothing inside

You say "I have a xib file associated" but the problem is that you don't. The runtime is looking for a xib file called ShowDetailsArtistAlbumViewController.xib but you don't have one. You need to rename your xib file to match the expectations of the runtime.
Alternatively, you may be creating your view controller incorrectly. You are saying this:
ShowDetailsArtistAlbumViewController *vc =
[[ShowDetailsArtistAlbumViewController alloc]
initWithNibName:#"ShowDetailsArtistAlbumViewController" bundle:nil];
That is why the runtime thinks you have a nib called ShowDetailsArtistAlbumViewController.xib - you are telling it that, right there in that line!
That's fine if there is in fact a nib called ShowDetailsArtistAlbumViewController.xib. Your problem is there is not. You have lied to the runtime and so naturally it crashes.
If you actually wanted to use the view controller in the storyboard, that's not how to access that instance. What you are doing is creating a different instance. If you want the instance in the storyboard, you need to talk to the storyboard and tell it to instantiateViewControllerWithIdentifier:.

Related

Call a view controller programatically using storyboard id iOS

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

Segueing a view without Storyboard

I'm trying to figure out how to preform transitions between different views just with code, without storyboard. For example, on a button press.
- (IBAction)nextClass
{
// insert transition between views
}
I know this is possible, I was just wondering what the actual code would be to make this happen.
Not 100% sure if I am understanding your question correctly, but I think you can just say:
ViewController *vc = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
The name "ViewController" after initWithNibName should be the name of the .xib file that contains the UI for ViewController. Let me know if this is what you are looking for!
EDIT:
Mungbeans brings up a good point. If you are using a navigation controller, you should say:
ViewController *vc = [[ViewController alloc]
initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];

Load UISplitViewController programmatically from another Storyboard

Currently I am using two iPad storyboards in my project.
The first storyboard has a Login screen and a tableview controller. I want to call the second storyboard from the first storyboard tableview controller, when the cell is clicked. Normally it's easy, but here the second story board has a UISplitViewController.
MainSVC *baseView = [[MainSVC alloc] init]; //UISplitViewController
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Mail_iPad" bundle:nil]; //Second Storyboard
baseView =[storyBoard instantiateViewControllerWithIdentifier:#"MainSVC"]; //MainSVC = Storyboard Name
[self presentViewController:baseView animated:YES completion:nil];
This code is not working. I searched in google, but couldn't find the best solution.
How can I call second storyboard splitview controller programmatically?
Use this code. This should be work. !
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Mail_iPad" bundle:nil];
UISplitViewController *split = [storyBoard instantiateViewControllerWithIdentifier:#"MainSVC"];
self.view.window.rootViewController = split;
Do not allocate another instance of split view controller. It should be done like this
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Mail_iPad" bundle:nil]; //Second Storyboard
MainSVC *baseView =(MainSVC *)[storyBoard instantiateViewControllerWithIdentifier:#"MainSVC"]; //MainSVC = Storyboard Name
[self presentViewController:baseView animated:YES completion:nil];

storyboard, self presentViewController, iOS 6, blank screen

I am using storyboards, I have one viewcontroller and on click I need to show another view controller modally. I am trying using this code
[self presentViewController:zoomV animated:YES completion:NULL];
I am coming up with a blank screen.
This is how I create
zViewController *zoomV = [[zViewController alloc] init];
[self presentViewController:zoomV animated:YES completion:NULL];
I tried researching this and some answers revolve around using storyboards and not having a rootviewcontroller associated. So what I have is in the initial scene I have a navigationController, and from there I drag to another Viewcontroller a relationship which defines it as a rootViewcontroller. Is that sufficient ? or is this irrelevant?
Since you have your zViewController in your storyboard, you should instantiate your zViewController using UIStoryboard instantiateViewControllerWithIdentifier:.
In your first view controller, instead of creating the zViewController using alloc/init do this, of course setting an identifier for your zViewController in your storyboard.
zViewController *zoomV = [self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
[self presentViewController:zoomV
animated:YES
completion:NULL];
Also you could accomplish the same using a segue and executing it directly, without the need of instantiating the zViewController, but is up to you.
As a second(small) comment, do not name classes starting with lowercase in ObjC :).
You may refer below snippet for story board:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
AddNameViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"AddNameViewController.m"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:sfvc animated:YES completion:nil];

how to transit to a view controller

I made a view controller A in storyboard and linked with other view controller. Now I have another view controller B and I want to write a method that can transit from B to A. How can I do this?
There is a segue to controller A in storyboard.
And controller B is loaded from another view I created programmatically so I can't set a segue in storyboard.
Since you are using storyboard. This is more accurate, try this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"YOUR VIEW CONTROLLER"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
But be sure to name your view's identifier otherwise it will crash and wont work.
Then when you want to dimiss the view:
[self dismissModalViewControllerAnimated:YES];

Resources