Kodeco Forums

Learn to Code iOS Apps 3: Your First App

In this tutorial, you will create a simple iOS game where you have to tap a button as many times as you can in 30 seconds. Just don't get too excited and smash your screen by mistake!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2783-learn-to-code-ios-apps-3-your-first-app

In IOS 8, UIAlertView is deprecated. I am using ios 8, apple doc hints that UIAlertController is a good one. Can you show me the way to use it and the sample code, please

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.

Thanks for ur series of the post, it really helps me a lot. Best wishes!:slight_smile:

Now I am using XCode 8.1.2 and there is not Round Rect Button in mainstoryboard. What can I do in this case?

Thank you in advance.

The only real difference is appearance - the new ā€œButtonā€ item
is pretty much the same, but in the style adopted since iOS7 it does not have a border by default. Both are ā€œUIButtonā€ when it comes to the code created and the IBAction/IBOutlet handling used so following the tutorial with that item instead will work fine.

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]