I'm converting my code from using UIActionSheet to use UIAlertController.
The way I do it using UIActionSheet is like this:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Gender"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSDictionary *genderInfo in self.genderList) {
NSString *gender = [[genderInfo objectForKey:#"description"] capitalizedString];
[actionSheet addButtonWithTitle:gender];
}
[actionSheet addButtonWithTitle:#"Cancel"];
And just handle what button is pressed on the delegate method of action sheet.
While converting it to alert controller, I noticed that there is a handler on each of the alert action. I wonder how will I implement the alert controller to have dynamic buttons that I can handle the actions.
Here i mention code for load array into UIAlertController dynamically with image & text :Output Image here
NSArray *numbersArrayList = #[#"One", #"Two", #"Three", #"Four", #"Five", #"Six"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Numbers:"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
for (int j =0 ; j<numbersArrayList.count; j++){
NSString *titleString = numbersArrayList[j];
UIAlertAction *action = [UIAlertAction actionWithTitle:titleString style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
NSLog(#"Selected Value: %#",numbersArrayList[j]);
}];
[action setValue:[[UIImage imageNamed:#"USA16.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:#"image"];
[alertController addAction:action];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
Got it!
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Gender:"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
for (NSDictionary *genderInfo in self.genderList) {
NSString *gender = [[genderInfo objectForKey:#"description"] capitalizedString];
UIAlertAction *action = [UIAlertAction actionWithTitle:gender
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSString *title = action.title;
//you can check here on what button is pressed using title
}];
[alertController addAction:action];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
In Swift:
import Foundation
import UIKit
class AlertUtility {
static let CancelButtonIndex = -1;
class func showAlert(_ onController:UIViewController, title:String?,message:String? ) {
showAlert(onController, title: title, message: message, cancelButton: "OK", buttons: nil, actions: nil)
}
/**
- parameter title: title for the alert
- parameter message: message for alert
- parameter cancelButton: title for cancel button
- parameter buttons: array of string for title for other buttons
- parameter actions: action is the callback which return the action and index of the button which was pressed
*/
class func showAlert(_ onController:UIViewController?, title:String?,message:String? = nil ,cancelButton:String = "OK",buttons:[String]? = nil,actions:((_ alertAction:UIAlertAction,_ index:Int)->())? = nil) {
// make sure it would be run on main queue
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action = UIAlertAction(title: cancelButton, style: UIAlertAction.Style.cancel) { (action) in
alertController.dismiss(animated: true, completion: nil)
actions?(action,CancelButtonIndex)
}
alertController.addAction(action)
if let _buttons = buttons {
for button in _buttons {
let action = UIAlertAction(title: button, style: .default) { (action) in
let index = _buttons.index(of: action.title!)
actions?(action,index!)
}
alertController.addAction(action)
}
}
if let onController = onController{
onController.present(alertController, animated: true, completion: nil)
}else{
// let appdel = UIApplication.shared.delegate as! AppDelegate
// appdel.window!.rootViewController?.present(alertController, animated: true, completion: nil)
}
}
}
Use in your VC:
//MARK:- IB-ACTION(S)
#IBAction func actionShowAlertButtonTapped(_ sender: Any) {
AlertUtility.showAlert(self, title: "Are you sure you want to start this order ?", cancelButton: "Cancel", buttons: ["OK", "Button 1", "Button 2", "Button 3", "Button 4","Button 5"], actions: {(action, index) -> () in
if index != AlertUtility.CancelButtonIndex {
//Cancel Button Action Here..
}else{
//Other Button Actions as per Button Index..
}
})
}
Related
This question already has an answer here:
UIActionsheet in Ios8
(1 answer)
Closed 6 years ago.
I want to use UIActionSheet for iOS8 but it's deprecated and I don't know how to use the updated way to use this...
See the old code:
-(void)acoesDoController:(UIViewController *)controller{
self.controller = controller;
UIActionSheet *opcoes = [[UIActionSheet alloc]initWithTitle:self.contato.nome delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Delete" otherButtonTitles:#"other", nil];
[opcoes showInView:controller.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//switch case of the buttons
}
Just to make clear, in this example the action sheet is activated after a long press in an UITableView index.
How can I implement the code above in the properly way?
You can use UIAlertController for the same.
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:#"Action Sheet" message:#"alert controller" preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// Distructive button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
Note : Please Find the answer in Swift as well.
var actionSheet = UIAlertController(title: "Action Sheet", message: "alert controller", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
// Cancel button tappped.
self.dismiss(animated: true) {
}
}))
actionSheet.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { action in
// Distructive button tapped.
self.dismiss(animated: true) {
}
}))
actionSheet.addAction(UIAlertAction(title: "Other", style: .default, handler: { action in
// OK button tapped.
self.dismiss(animated: true) {
}
}))
// Present action sheet.
present(actionSheet, animated: true)
// Answer for SwiftUI
struct ContentView: View {
#State private var showingOptions = false
#State private var selection = "None"
var body: some View {
VStack {
Text(selection)
Button("Confirm paint color") {
showingOptions = true
}
.confirmationDialog("Select a color", isPresented: $showingOptions, titleVisibility: .visible) {
Button("Red") {
selection = "Red"
}
Button("Green") {
selection = "Green"
}
Button("Blue") {
selection = "Blue"
}
}
}
}
}
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?
- (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];
}
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?
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)
Create global Alert controller that is accessible in all view controllers using extension.
Create an extension of UIViewController with your function to display alert with 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>)
}
}
How can I link UIAlertController alert action buttons with action handlers using objective-c? I'm using Xcode 7.1.
Here is my code:
- (IBAction)selectbtn:(UIButton *)sender {
UIAlertController *alert=[ UIAlertController alertControllerWithTitle:#"NEW" message:#"button pressed" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraaction=[UIAlertAction actionWithTitle:#"From camera" style:UIAlertActionStyleDefault handler:nil ];
[alert addAction:cameraaction];
UIAlertAction *libraryaction=[UIAlertAction actionWithTitle:#"From photo library" style:UIAlertActionStyleDefault handler:nil ];
[alert addAction:libraryaction];
UIAlertAction *cancelaction=[UIAlertAction actionWithTitle:#"cancel" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction:cancelaction];
[self presentViewController:alert animated:YES
completion:nil];
}
Objective-C
UIAlertController works like this:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"text mssg" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Ok action example
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:#"Other" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Other action
}];
[alert addAction:okAction];
[alert addAction:otherAction];
[self presentViewController:alert animated:YES completion:nil];
I think you meant that.
Swift 3.0/4.0
let myalert = UIAlertController(title: "Titulo mensaje", message: "Mi mensaje.", preferredStyle: UIAlertControllerStyle.alert)
myalert.addAction(UIAlertAction(title: "Aceptar", style: .default) { (action:UIAlertAction!) in
print("Selected")
})
myalert.addAction(UIAlertAction(title: "Cancelar", style: .cancel) { (action:UIAlertAction!) in
print("Cancel")
})
self.present(myalert, animated: true)
Swift 5
let myalert = UIAlertController(title: "Titulo mensaje", message: "Mi mensaje.", preferredStyle: UIAlertController.Style.alert)
myalert.addAction(UIAlertAction(title: "Aceptar", style: .default) { (action:UIAlertAction!) in
print("Selected")
})
myalert.addAction(UIAlertAction(title: "Cancelar", style: .cancel) { (action:UIAlertAction!) in
print("Cancel")
})
self.present(myalert, animated: true)
You can add any code you want in the handler of the action method, sample code can be like this:
#interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (strong, nonatomic) UIAlertController *alertCtrl;
#property (strong, nonatomic) UIImagePickerController *imagePicker;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupAlertCtrl];
}
- (void) setupAlertCtrl
{
self.alertCtrl = [UIAlertController alertControllerWithTitle:#"Select Image"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
//Create an action
UIAlertAction *camera = [UIAlertAction actionWithTitle:#"From camera"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self handleCamera];
}];
UIAlertAction *imageGallery = [UIAlertAction actionWithTitle:#"From Photo Library"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self handleImageGallery];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[self dismissViewControllerAnimated:YES completion:nil];
}];
//Add action to alertCtrl
[self.alertCtrl addAction:camera];
[self.alertCtrl addAction:imageGallery];
[self.alertCtrl addAction:cancel];
}
- (IBAction)selectImagePressed:(UIButton *)sender
{
[self presentViewController:self.alertCtrl animated:YES completion:nil];
}
- (void)handleCamera
{
#if TARGET_IPHONE_SIMULATOR
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Error"
message:#"Camera is not available on simulator"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
#elif TARGET_OS_IPHONE
//Some code for iPhone
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePicker animated:YES completion:nil];
#endif
}
- (void)handleImageGallery
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.delegate = self;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
UIImage *img = [[UIImage alloc] initWithData:dataImage];
[self.imageView setImage:img];
[self.imagePicker dismissViewControllerAnimated:YES completion:nil];
}
Ref Link
UIAlertView Controller with multiple textFields.
#IBAction func showAlert(_ sender: UIButton) {
let alert = UIAlertController(title: "Registration", message: "Enter your name!", preferredStyle: .alert)
alert.addTextField { (textField1: UITextField) in
textField1.placeholder = "John"
}
alert.addTextField { (textField2: UITextField) in
textField2.placeholder = "Doe"
}
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction) in
if let textField1 = alert.textFields?.first {
if textField1.text == "" {
print("you have to enter something first")
}else {
print("Hello \(textField1.text!)")
}
}
if let textField2 = alert.textFields?.first {
if textField2.text == "" {
print("you have to enter something first")
}else {
print("Hello \(textField2.text!)")
}
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
I want to add text input in alert-view of ios 8.
I know it was done using UIAlertController but not have any idea.
How to do it ?
Screenshot
Code
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: #"Login"
message: #"Input username and password"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"name";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"password";
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.secureTextEntry = YES;
}];
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSArray * textfields = alertController.textFields;
UITextField * namefield = textfields[0];
UITextField * passwordfiled = textfields[1];
NSLog(#"%#:%#",namefield.text,passwordfiled.text);
}]];
[self presentViewController:alertController animated:YES completion:nil];
AlertViewController
// use UIAlertController
UIAlertController *alert= [UIAlertController
alertControllerWithTitle:#"Title"
message:#"SubTitle"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action){
//Do Some action here
UITextField *textField = alert.textFields[0];
NSLog(#"text was %#", textField.text);
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSLog(#"cancel btn");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"placeHolderText";
textField.keyboardType = UIKeyboardTypeDefault;
}];
[self presentViewController:alert animated:YES completion:nil];
UIAlertView
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"SubTitle"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
dialog.alertViewStyle = UIAlertViewStylePlainTextInput;
[dialog show];
}
Example of implementation with Swift 3:
var textField: UITextField?
// create alertController
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alertController.addTextField { (pTextField) in
pTextField.placeholder = "usefull placeholdr"
pTextField.clearButtonMode = .whileEditing
pTextField.borderStyle = .none
textField = pTextField
}
// create cancel button
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (pAction) in
alertController.dismiss(animated: true, completion: nil)
}))
// create Ok button
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { (pAction) in
// when user taps OK, you get your value here
let inputValue = textField?.text
alertController.dismiss(animated: true, completion: nil)
}))
// show alert controller
self.present(alertController, animated: true, completion: nil)
UIAlertView *myView = [[UIAlertView alloc]initWithTitle:#"Input" message:#"Enter your value" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
myView.alertViewStyle = UIAlertViewStylePlainTextInput;
[myView textFieldAtIndex:0].delegate = self;
[myView show];
you can cover this way .Thanks
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Title" message:#"Please enter someth" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
also, you will need to implement UITextFieldDelegate, UIAlertViewDelegate protocols.
and if you user uialertcontroller then use this one
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"My Title"
message:#"Enter User Credentials"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Do Some action here
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Username";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = #"Password";
textField.secureTextEntry = YES;
}];
[self presentViewController:alert animated:YES completion:nil];
Here's handy method with submit/cancel and completion handler for text if inputted:
/**
Presents an alert controller with a single text field for user input
- parameters:
- title: Alert title
- message: Alert message
- placeholder: Placeholder for textfield
- completion: Returned user input
*/
func showSubmitTextFieldAlert(title: String,
message: String,
placeholder: String,
completion: #escaping (_ userInput: String?) -> Void) {
let alertController = UIAlertController(title: title,
message: message,
preferredStyle: .alert)
alertController.addTextField { (textField) in
textField.placeholder = placeholder
textField.clearButtonMode = .whileEditing
}
let submitAction = UIAlertAction(title: "Submit", style: .default) { (action) in
let userInput = alertController.textFields?.first?.text
completion(userInput)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in
completion(nil)
}
alertController.addAction(submitAction)
alertController.addAction(cancelAction)
present(alertController, animated: true)
}
This one is work for me:
let passwordString = lableGetPassword.text
var textField: UITextField?
// create alertController
let alertController = UIAlertController(title: "Password", message: "Save the password. Give a tag name.", preferredStyle: .alert)
alertController.addTextField { (pTextField) in
pTextField.placeholder = "Tag Name"
pTextField.clearButtonMode = .whileEditing
pTextField.borderStyle = .none
textField = pTextField
}
// create cancel button
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (pAction) in
alertController.dismiss(animated: true, completion: nil)
}))
// create Ok button
alertController.addAction(UIAlertAction(title: "Save", style: .default, handler: { [self] (pAction) in
// when user taps OK, you get your value here
let name = textField?.text
save(name: name!, password: passwordString!)
alertController.dismiss(animated: true, completion: nil)
}))
// show alert controller
self.present(alertController, animated: true, completion: nil)
UIAlertViewController with text input
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// optionally configure the text field
textField.keyboardType = UIKeyboardTypeAlphabet;
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
UITextField *textField = [alert.textFields firstObject];
textField.placeholder = #"Enter Input";
}];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
func askForName() {
let alert = UIAlertController(title: "Enter Text",
message: "Enter some text below",
preferredStyle: .alert)
alert.addTextField { (textField) in
textField.text = "New Text"
}
let action = UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert!.textFields![0]
print("Text field: \(textField.text)")
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
Previous to iOS8 we used the UIActionSheet for showing alert and now we need to use the UIAlertController.
When we used the UIActionSheet we could easily handle situations where the user clicked outside the pop up (which means he want to cancel the operation) by comparing the clickedButtonAtIndex to the cancelButtonIndex - if the user indeed pressed outside the popup we got the cancel button index in this function.
How can we handle these situations with the new UIAlertController? I tried to use the "completion" block but it doesn't have any context. Is there an easy way to handle this? (other than "saving" the actions states in some general variable).
You can add an action with style:UIAlertActionStyleCancel and the handler for this action is called when the user taps outside the popup.
if ([UIAlertController class]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Alert Title" message:#"A Message" preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(#"User clicked button called %# or tapped elsewhere",action.title);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(#"User clicked button called %#",action.title);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:#"Other" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(#"User clicked button called %#",action.title);
}]];
UIControl *aControl = (UIControl *) sender;
CGRect frameInView = [aControl convertRect:aControl.bounds toView:self.view];
alertController.popoverPresentationController.sourceRect = frameInView;
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:alertController animated:YES completion:nil];
}
The solution which works for UIAlertController with alert style. Just needed to add gesture recognizer to alertController superview.
[self presentViewController: alertController
animated: YES
completion:^{
alertController.view.superview.userInteractionEnabled = YES;
[alertController.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(alertControllerBackgroundTapped)]];
}];
- (void)alertControllerBackgroundTapped
{
[self dismissViewControllerAnimated: YES
completion: nil];
}
UITapGestureRecognizer didn't work for me so I've used this way:
func addDismissControl(_ toView: UIView) {
let dismissControl = UIControl()
dismissControl.addTarget(self, action: #selector(self.dismissAlertController), for: .touchDown)
dismissControl.frame = toView.superview?.frame ?? CGRect.zero
toView.superview?.insertSubview(dismissControl, belowSubview: toView)
}
func dismissAlertController() {
self.dismiss(animated: true, completion: nil)
}
func presentAlertController(title: String?, message: String?, preferredStyle: UIAlertControllerStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil, completion: (() -> Swift.Void)? = nil) {
let alertController = UIAlertController(title: title, message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "OK", style: .default) { (alertAction) -> Void in
handler?(alertAction)
})
self.present(alertController, animated: true, completion: {
self.addDismissControl(alertController.view)
completion?()
})
}
func someWhereInYourViewController() {
// ...
presentAlertController(title: "SomeTitle", message: "SomeMessage", preferredStyle: .actionSheet, handler: { (alertAction) -> Void in
//do some action
}, completion: {
//do something after presentation
})
// ...
}