IOS creates UIAlertViewController program code

I am working on a ViewController with code (without storyboard). I am trying to add an AlertController

I declare myself in .m

@property (nonatomic, strong) UIAlertController *alertController; 

And init in loadview method

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

And call 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 do not know why the warning does not appear. Something is wrong with my code. How to configure alertViewController program call?

+12
source share
4 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]; } 
+49
source

And in Swift> = 3 :

  let alertController = UIAlertController(title: "Some Error", message: "Pleas confirm?", preferredStyle: .alert) let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(defaultAction) self?.present(alertController, animated: true, completion: nil) 
+2
source

In Xcode 9.4.1

Create an alert feature globally and use everywhere.

 //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 as it is in the place you need.

Import this class and instantiate

 //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?

+2
source

Create a global alert controller that is available in all view controllers using the extension.
Create a UIViewController extension with your function to display a warning with the required parameter arguments.

 extension UIViewController { func displayalert(title:String, message:String) { let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in alert.dismiss(animated: true, completion: nil) }))) self.present(alert, animated: true, completion: nil) } } 


Now call this function from your view controller:

 class TestViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.displayalert(title: <String>, message: <String>) } } 
0
source

All Articles