Unable to switch between UIViewControllers using storyboard - objective-c

I am new to objective C so please forgive me if this is a stupid question (and I have searched!)
I am using storyboards in xcode (4.4.1) and I have 2 UIViewControllers
I am trying to programmatically switch between them (not sure if that's the right terminology?)
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"BookingView"];
[self presentModalViewController:vc animated:YES];
The above code works perfectly later on in my project, but I when i try call if in an if() in my viewdidload method in the first UIViewController it doesn't work. Help please!

You shouldn't perform such task in the viewDidLoad: method.
You're trying to present a view controller modally on top of another that's only been allocated in memory and loaded, but it's not visible yet. That means your presentation won't work. You should move your code to viewDidAppear:
I recommend you to read this guide to learn the basics about view controller delegation: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40007457

Why not self.storyboard instead of [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];?
Assuming that all your view controllers are from the same storyboard :)

Related

Can't modal to next viewcontroller

I feel like I am just missing something simple here. I am trying to modal to my next view controller. I imported the next view controller in my first .m file first. After I did that I wrote this code
CRHViewController *nextViewController = [[CRHViewController alloc]init];
[self presentModalViewController:nextViewController animated:NO];
Also, I am working with storyboard and not nibs.
What happens when I run this is as soon as it goes to modal to the next viewcontroller it just goes black.
Am I missing something simple? Does anyone have an suggestions to fix this problem?
Probably you're not initialising it correctly. For testing purposes I would try to display CRHViewController as first root viewController from AppDelegate and find if it is initialising at all. Then check if it gets to its methods:
initWithNibName
loadView
awakeFromNib
viewDidLoad
viewWillAppear
viewDidAppear
In this order. Its 90% certain that one of them fails. Check if it gets to every method you have implemented correctly in this order.
If your CRHViewController is in your storyboard, then you should be instantiating it with:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"yourStoryboardName" bundle:nil];
[storyboard instantiateViewControllerWithIdentifier:#"myIdentifier"];
You should give your view controller an identifier in IB to pass in as the identifier parameter in the above method.

how to access the UITabBarController when its not the initial view?

I'm having problem with accessing my tabbarcontroller in the storyboard when its not the initial view. So basically there is an initial view in the storyboard which leads to the tabbarcontroller. I want to change the color of the tab but i dont have access to it! I know that it can be added to the delegate if its the initial view but in this case its not the initial view in the storyboard! I read somewhere that I have to override a method in the first view but there was no detail about it!
If you are in a viewcontroller in the storyboard then this code:
UIStoryboard *storyboard = self.storyboard;
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"OtherViewController"];
Will get the other viewcontroller youw want and you could do things such as:
set yourself as delegate for it:
[(OtherViewController *)vc setDelegate:self];
display it:
[self presentViewController:vc animated:YES completion:nil];
I think you could use this to get the other viewcontroller and then access the tabbar from it.
Sorry if I misunderstood what you are trying to do.

Show view from storyboard chain

I have a chain in storyboard and I want to start (for example) second view when I have first launching app. I have some code, which only work in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
method, if I put it in
- (void)viewDidLoad
method it's not work,
the code which show my other view is:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"someId"];
[vc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
How can I show view, which have place in chain in storyboard when app have first launching?
Try your code in
-(void)viewDidAppear:(BOOL)animated.
For further information on the iOS viewcontroller lifecycle head over to UIViewController class reference
If you are using a navigation controller, you can simply push the second view onto the navigation stack. If you are not using a navigation controller, then you could force a segue to the next view from the loading.

storyboard and programic modal changes

starting a new project and want to try using the storyboard feature of xcode 4.2. i got the basics of how to do transitions within the story board, but how do i do it in the code (load a new view w/out the use of a button)?
do i just use the old method,
mapViewController *mapView = [[mapViewController alloc] initWithNibName:#"mapViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:mapView animated:YES];
?
EDIT:
the old way doesn't work. also tried:
MapViewController *mapView = [self.storyboard instantiateViewControllerWithIdentifier:#"MapView"];
self presentModalViewController:mapView animated:YES];
with no success.
If you want to load a view controller you made in a storyboard create a segue to this view controller, give it an identifier and call: performSegueWithIdentifier:sender:.
ok, i figured out the issue. i had uncommented the method "loadView" in the mapViewController so xcode did not use my storyboard layout.
also, both above methods do work to do a modal switch.

iOS 5 Storyboards and Nibs

I have started my iOS 5 app using a storyboard, however if I want to programatically modally present a view, how can I? I can't use initWithNibName as there are no longer nob files, but a storyboard.
E.g. this will give me a blank UINavigationView and not my Interface Builder one:
setupView = [[setupController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:setupView];
[self presentModalViewController:navigationController animated:YES];
If I use a button in interface builder (in my storyboard) and link the two views with it using 'Modal' it works a charm, but I want to do it programatically.
Thanks.
Turns out I think I have been looking for performSegueWithIdentifier and it works a charm.
Beginning Storyboards in iOS 5 Part 1 explained everything in details.