UIAlertController won't display - In Swift
The problem is that by the time you call this following function:
present(alertController, animated: true, completion: nil)
your mail view controller is still visible. You have to make sure that the alertController
is presented on the top of window hierarchy. In your example, you have the following window hierarchy:
+---------------------------+
| MailComposeViewController |
+---------------------------+
||
+---------------------------+
| ContactUsViewController |
+---------------------------+
What you should do instead is to dismiss the email view controller first. When that is done, show you alert view controller.
let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
alertController.addAction(okButton)
controller.dismissViewControllerAnimated(true){ () -> Void in
self.present(alertController, animated: true, completion: nil)
}
Alternatively, you can also present your alertController
on top of the MailComposeViewController
, like so:
let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
alertController.addAction(okButton)
controller.present(alertController, animated: true, completion: nil)
UIAlertController with UITextfield does not display Title and Message
Here it is the code for create UIAlertController
for showing Message and title.
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Alert Controller"
message:@"Alert Message"
preferredStyle:UIAlertControllerStyleAlert];
UIViewController *viewController = [[UIViewController alloc]init];
[viewController.view setBackgroundColor:[UIColor blueColor]];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 250, 30)];
lbl.text = @"This is a label";
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor whiteColor];
[viewController.view addSubview:lbl];
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 35, 250, 30)];
tf.borderStyle = UITextBorderStyleRoundedRect;
tf.placeholder = @"Enter your name";
[viewController.view addSubview:tf];
[alertController setValue:viewController forKey:@"contentViewController"];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
NSLog(@"Text Value : %@",tf.text);
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alertController animated:YES completion:nil];
});
For swift you can do same thing like following code:
let alert = UIAlertController(title: "Alert Controller", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(
title: "Cancel",
style: UIAlertActionStyle.Destructive) { (action) in
}
let confirmAction = UIAlertAction(
title: "OK", style: UIAlertActionStyle.Default) { (action) in
}
alert.addAction(confirmAction)
alert.addAction(cancelAction)
let VC = UIViewController()
VC.view.backgroundColor = UIColor.blackColor()
let lbl = UILabel()
lbl.frame = CGRectMake(10, 8, 250, 30)
lbl.text = "this is a label"
lbl.textAlignment = NSTextAlignment.Center
lbl.textColor = UIColor.whiteColor()
VC.view .addSubview(lbl)
let txt = UITextField()
txt.frame = CGRectMake(10, 35, 250, 30)
txt.borderStyle = UITextBorderStyle.RoundedRect
txt.placeholder = "enter text"
VC.view .addSubview(txt)
alert.setValue(VC, forKey: "contentViewController")
self.presentViewController(alert, animated: true, completion: nil)
Download Demo from Github: https://github.com/nitingohel/NGAlertViewController-Swift2.0
Related Topics
Get "No Keychain Available" Error When Try to Access Keychain from App Extension
How to Change Uiwebview Orientation Programmatically in Swift
How to Send Request from iOS (Swift) to Dialogflow V2 API Without Cloud Functions
How to Use Tap Gesture in Accessibility in Swift
Draw Button on Top of Avplayer
Swift 2 Cannot Invoke 'Fseventstreamcreate' with an Argument List of Type
Exception: Cannot Manually Set the Delegate on a Uinavigationbar Managed by a Controller
Error in Facebook Login, iOS 9, Swift
Get Total Include Months Between 2 Date in Swift
How to Add Storyboard Viewcontroller into Swiftui Project
Extract 4 Bits of Bluetooth Hex Data
Replace C Style For-Loop in Swift 2.2.1
Multiple Image Pickers in One Controller
How to Open a Local PDF File Using a Swiftui Button
Swift Conform to Protocol Subclass
Swift Parse - Local Datastore and Displaying Objects in a Tableview
Nsexceptionallowsinsecurehttploads Not Working for Ip Addresses