Yeah, so UIAlertView got deprecated in iOS8. Some Googling and the ever-present Stack Overflow, along with the Apple docs, provide some excellent answers though. In short, this is Mikeās original code:
// The code inside the subtractTime() function
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time is up!"
message:[NSString stringWithFormat:@"You scored %i points", count]
delegate:self
cancelButtonTitle:@"Play Again"
otherButtonTitles:nil];
[alert show];
// The function that is called if the 'Play again' button is pressed
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self setupGame];
}
From iOS8, most of the above is deprecated. If you check Appleās docs, together with the SO answer, youāll get an excellent explanation on what to do instead. Iāve placed the correct code below, hope it helps you too. Check the links if you want more info. If you just want to copy paste, just replace the UIAlertView part with the below stuff:
First, we have to create the actual popup alert. From iOS8, we do this with the UIAlertController class. Itās pretty similar to the original UIAlertView as youāll notice, with a title, a message, and a preferred styling of the look of the popup.
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Time is up!"
message:[NSString stringWithFormat:@"Your score was %i", count]
preferredStyle:UIAlertControllerStyleAlert];
However, you will notice that the above code only created a popup, but no button! Thatās what from iOS8 onwards is called an āactionā. So below, we create the action that we want our popup to have. Again, we give it a title, a style, but hereās the different part, we give it a handler. If you read Mikeās page again, youāll notice he mentions that, once the button is pressed, we should hand-off control to the ViewController. Thatās effectively what weāre doing here. As you can see, weāre calling the [self setupGame] function!
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Play again"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self setupGame];
}];
Now that we have created the alert and the action, we should of course link the two together:
[alert addAction:defaultAction];
And lastly, we actually show the alert on the screen (make it pop up)
[self presentViewController:alert animated:YES completion:nil];
Additional note: You can remove the <UIAlertViewDelegate>
tag in ViewController.h
, as that is no longer necessary with this new way of declaring variables (as far as I understand it anyways, I just removed it and the code still runs fine).
Hope this helps you too.