EA Rashel EA Rashel - 1 year ago 188
Objective-C Question

Using UIAlertController in Xcode 8 with Objective-C

I am trying to show a

every time the user start the app.

- (void)viewDidLoad {
[super viewDidLoad];

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"My alert" message:@"This should be come when the app start" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *yesButton = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];

[alert addAction:yesButton];
[self presentViewController:alert animated:YES completion:nil];

But this is not working. I run the app in Xcode simulator. The app runs but no alertview is showing. What am I doing wrong?

Answer Source

Try opening the UIAlertController in the viewDidAppear: delegate instead. Alternatively, you could show it as part of your AppDelegate method didFinishLaunchingWithOptions: to make it more view-controller independent.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download