Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS create UIAlertViewController programmatically

Tags:

I'm working on a ViewController with code (no storyboard). I'm trying to add and AlertController

I have declare propery in .m

@property (nonatomic, strong) UIAlertController *alertController; 

And init in loadview method

//alertviewController     _alertController = [[UIAlertController alloc]initWithNibName:nil bundle:nil]; 

And call the alertview in viewDidLoad:

_alertController = [UIAlertController alertControllerWithTitle:@"Error display content" message:@"Error connecting to server, no local database" preferredStyle:UIAlertControllerStyleAlert];  UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {                 LandingPageViewController *viewController = [[LandingPageViewController alloc] initWithNibName:nil bundle:nil];     //            viewController.showNavBarBackButton = YES;                 [[AppDelegate sharedAppDelegate].rootViewController cPushViewController:viewController]; }]; [_alertController addAction:ok]; [self presentViewController:_alertController animated:YES completion:nil]; 

I dont' know why the alert is not showing up. Some thing wrong with my code. How to set up and call alertViewController programmatically?

like image 629
Lê Khánh Vinh Avatar asked Aug 01 '16 15:08

Lê Khánh Vinh


2 Answers

- (void)logoutButtonPressed {      UIAlertController * alert = [UIAlertController                                  alertControllerWithTitle:@"Logout"                                  message:@"Are You Sure Want to Logout!"                                  preferredStyle:UIAlertControllerStyleAlert];      //Add Buttons      UIAlertAction* yesButton = [UIAlertAction                                 actionWithTitle:@"Yes"                                 style:UIAlertActionStyleDefault                                 handler:^(UIAlertAction * action) {                                     //Handle your yes please button action here                                     [self clearAllData];                                 }];      UIAlertAction* noButton = [UIAlertAction                                actionWithTitle:@"Cancel"                                style:UIAlertActionStyleDefault                                handler:^(UIAlertAction * action) {                                    //Handle no, thanks button                                }];      //Add your buttons to alert controller      [alert addAction:yesButton];     [alert addAction:noButton];      [self presentViewController:alert animated:YES completion:nil]; } 
like image 142
ChenSmile Avatar answered Sep 18 '22 17:09

ChenSmile


In Xcode 9.4.1

Create alert function globally and use every where.

//Alert function - (void) showAlertMsg:(UIViewController *)viewController title:(NSString *)title message:(NSString *)message {      UIAlertController * alert = [UIAlertController alertControllerWithTitle : title                                                                     message : message                                                              preferredStyle : UIAlertControllerStyleAlert];      UIAlertAction * ok = [UIAlertAction                           actionWithTitle:@"OK"                           style:UIAlertActionStyleDefault                           handler:^(UIAlertAction * action)                           { }];      [alert addAction:ok];     dispatch_async(dispatch_get_main_queue(), ^{         [viewController presentViewController:alert animated:YES completion:nil];     });  } 

Call like this in your required place.

Import that class and create instance

//Ex: GlobalClassViewController *gcvc = [[GlobalClassViewController alloc]init];  //Call alert function with instance [gcvc showAlertMsg:self title:@"" message:placeholderText]; 

How would I create a UIAlertView in Swift?

like image 26
Naresh Avatar answered Sep 20 '22 17:09

Naresh