Lê Khánh Vinh Lê Khánh Vinh - 4 months ago 126
Objective-C Question

IOS create UIAlertViewController programatically

Hi 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
programatically? any help is much appreciate! Thanks

Answer

And in Swift:

let alertController = UIAlertController(title: "Things are happening", message: "Pleas confirm?", preferredStyle: .Alert)

let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
        alertController.addAction(defaultAction)

presentViewController(alertController, animated: true, completion: nil)