Frank Tocci Frank Tocci - 5 months ago 80
Objective-C Question

New to IOS/Objective C Development; UIAlertView/Controller issues

I'm trying to make my first iOS app, and the tutorial asked me to use UIAlertView. XCode informed me that this was deprecated and that I should use UIAlertController. I replaced the instances of UIAlertView with UIAlertController and am still running into problems: it says, "No visible @interface for 'UIAlertController' declares the selector 'initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:'
I would appreciate any help with this. My code is below:

//
// ViewController.m
// cv
//
// Created by Frank Michael Tocci on 6/17/16.
// Copyright © 2016 Frank Tocci. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIAlertController * alert = [[UIAlertController alloc] initWithTitle:@"Hello!" message:@"Welcome to OpenCV" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
[alert show];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Answer

UIAlertController is not UIAlertView

The way you initialize it and display it is different. Here's how:

// Init the alert
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                              message:@"This is an alert."
                                              preferredStyle:UIAlertControllerStyleAlert];

// Setup the action for the alert
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" 
                                              style:UIAlertActionStyleDefault
                                              handler:^(UIAlertAction * action) {}];

// Add the defaultAction to the alert (this will add an action button with title "OK" to the alert)
[alert addAction:defaultAction];

// Display it
[self presentViewController:alert animated:YES completion:nil];