Reputation: 3437
I am trying to load a UIViewController on the same storyboard, I have given the View Controller a Storyboard ID, Restoration ID (Same as Storyboard ID) and View Controller Title of 'breakdown'.
I have an action registered as getStarted in which I am running the following code, I am also importing the view controller I want to load inside the view controller with the Get Started button
#import "BreakdownViewController.h"
'
- (IBAction)getStarted:(id)sender {
NSLog(@"Yes");
BreakdownViewController *controller = [[BreakdownViewController alloc] initWithNibName:@"breakdown" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
}
Yes logs out but the view controller doesn't load.
Any ideas why?
Upvotes: 0
Views: 2629
Reputation: 87
If I understand the question correctly, you have a view controller defined in the same storyboard as the one you are trying to push from? Why aren't you using a segue? Create a push segue from the source to the destination and have the button trigger that segue.
Once the segue is wired up, you could programmatically invoke the push with:
[self performSegueWithIdentifier:@"breakdownSegue"];
But if you wire the segue directly to the button you want to invoke the segue, you can skip that step.
If the problem is that BreakdownViewController is defined in an external .xib and you added it to this storyboard, just remove the view from the storyboard scene and it will load the one from the .xib.
Upvotes: -1
Reputation: 12093
The view doesn't load because you are trying to load it from a nib file not from the storyboard. I am assuming that BreakdownViewController
is a subclass of UIViewController
or a controller that can be loaded properly. Change your code to this.
// You could change `BreakdownViewController` to `UIViewController` if it doesn't work with it.
// iOS 6.0 and above.
BreakdownViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"breakdown"];
[self presentViewController:controller animated:YES completion:nil];
Due to methods being deprecated you should really be using the code from above but you could also do.
// iOS 5.0
BreakdownViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"breakdown"];
[self presentModalViewController:controller animated:YES];
You could also add some transition animation to this as well buy adding the line below just before the presentModalViewController
or the presentViewController
[controller setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
You could also replace the 'presentModalViewController' or presentViewController
line with
[self.navigationController pushViewController:controller animated:YES];
EDIT
After user used code provided above they got an error
2013-02-24 16:08:14.517 BudgetMe[1300:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFString stringByAppendingString:]: nil argument' *** First throw call stack: (0x1cac012 0x10e9e7e 0x1cabdeb 0xaf87d5 0xddba 0x10f1c7 0x10f232 0x11ac25 0x31a3a3 0x117ee3 0x118167 0x312c 0x10fd705 0x312c0 0x31258 0xf2021 0xf257f 0xf16e8 0x60cef 0x60f02 0x3ed4a 0x30698 0x1c07df9 0x1c07ad0 0x1c21bf5 0x1c21962 0x1c52bb6 0x1c51f44 0x1c51e1b 0x1c067e3 0x1c06668 0x2dffc 0x252d 0x2455)
This was getting thrown when it reached the [self.navigationController pushViewController...]
/ [self presentViewController...]
I believe that was error now getting thrown because of an issue on the view controller that the user was trying to push/present. The reason I believe this is because when this line of code is hit it tries to run methods like viewDidLoad
before moving onto the actual view controller so if an error is thrown it shows as if it is on this line instead. Best way to find this out is to take full advantage of break points.
I have suffered this problem before NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' when presenting view controller
Upvotes: 2
Reputation: 7717
You probably want to use this:
[self.storyboard instantiateViewControllerWithIdentifier:@"breakdown"];
Instead of the alloc initWithNibName.
Upvotes: 0