Reputation: 91
I'm new at Objective-C development and looking for a way to change from scene programmatically. I've searched a lot but I keep getting errors. Could anyone tell me what I'm doing wrong? Any help would be very appreciated. My code so far:
- (IBAction)retryOrNextLevel:(id)sender {
if (retryInteger == 1){
[self viewDidLoad];
}
if (retryInteger == 2){
Level2 *secondLevel = [[Level2 alloc] initWithNibName:@"Level2" bundle:nil];
[secondLevel setTitle:@"Level2"];
secondLevel.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:secondLevel animated:NO completion:Nil];
}
}
retryInteger is an integer that is set to 1 if you have failed the level and to 2 if you have completed the level of the game. I use the same button for two things: if you have failed the level you can replay the level and when you've completed the level you can go to the next level. Level2 is a UIViewController. Thanks in advance! Matis
Upvotes: 0
Views: 1115
Reputation: 91
I have found the answer! The error was because I forgot to declare something and the reason nothing happened when I touched the button was because I didn't drag a segue between the view controllers. This video answered my question: http://www.youtube.com/watch?v=XApYtAlRDVE.
Upvotes: 0
Reputation: 12123
First off, you should never call [self viewDidLoad];
this is called automatically when would you guess your viewDidLoad
.
Second what is retryInteger? I am going to assume for this answer it is some sort of value you have set if the user has already hit this button, or done something.
So lets see what we can do with your code :
- (IBAction)retryOrNextLevel:(id)sender {
if (retryInteger == 1){
[self callSomeMethodThatIsntVIEWDIDLOAD];
}
if (retryInteger == 2){
Level2 *secondLevel = [[Level2 alloc] initWithNibName:@"Level2" bundle:nil];
[secondLevel setTitle:@"Level2"];
// Pushing onto a navigationController would seem more logical (Would recommend)
// [self.navigationController pushViewController:secondLevel animated:YES];
// Otherwise you want this
[self presentViewController:secondLevel animated:YES completion:nil];
}
}
There are several things assumed in this answer and until you update your question this answer is based on assumptions. First of what is Level2
is it a UIViewController
? What error you are getting? As your original code should present the view controller so I am going to assume that when it tries to present it you are doing something in Level2
controller that is breaking it, cause if something goes wrong in Level2
controller then it will show on this line [self presentViewController:secondLevel animated:NO completion:Nil];
I would also make it so it is animated as if you say NO it makes this modalTransitionStyle
pointless.
If you update your question I will try to improve this answer but until you do there isn't much else I can do. If your app quits on crash then use an Exception Breakpoint
that is stops on when an exception is thrown.
EDIT
You are not calling [self retryOrNextLevel]
you want to be calling [self retryOrNextLevel:myButton]
your not calling the correct method. Where ever you are calling it you are missing the parameters so you are missing :myButton
.
Your method is
- (IBAction)retryOrNextLevel:(id)sender;
and not
- (IBAction)retryOrNextLevel;
note the sender parameter.
To make sure that the class responds to retryOrNextLevel
you can do
if([self respondsToSelector:@selector(retryOrNextLevel:)] {
// Code for calling the method.
}
Upvotes: 1