UIPopover How do I make a popover with buttons like this? - ios

I'm wondering how I can create a popover with buttons like this.
ANSWER:
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: #"Take Photo",
#"Choose Existing Photo", nil];
[actionSheet showFromRect: button.frame inView: button.superview animated: YES];
Somewhere else in your delegated object class...
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
// take photo...
}
else if (buttonIndex == 1) {
// choose existing photo...
}
}

This is a UIActionSheet. On the iPhone, it animates in from the bottom. On the iPad it appears in a popover.
Assuming you're doing this on the press of a button:
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate: self
cancelButtonTitle: nil
destructiveButtonTitle: nil
otherButtonTitles: #"Take Photo",
#"Choose Existing Photo", nil];
[actionSheet showFromRect: button.frame inView: button.superview animated: YES];
In iOS8+, you should use the new UIAlertController class:
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
message: nil
preferredStyle: UIAlertControllerStyleActionSheet];
[alertController addAction: [UIAlertAction actionWithTitle: #"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Handle Take Photo here
}]];
[alertController addAction: [UIAlertAction actionWithTitle: #"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// Handle Choose Existing Photo here
}]];
alertController.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController * popover = alertController.popoverPresentationController;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
popover.sourceView = sender;
popover.sourceRect = sender.bounds;
[self presentViewController: alertController animated: YES completion: nil];
or in Swift
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
alertController.addAction(UIAlertAction(title: "Take Photo", style: .Default, handler: { alertAction in
// Handle Take Photo here
}))
alertController.addAction(UIAlertAction(title: "Choose Existing Photo", style: .Default, handler: { alertAction in
// Handle Choose Existing Photo
}))
alertController.modalPresentationStyle = .Popover
let popover = alertController.popoverPresentationController!
popover.permittedArrowDirections = .Up
popover.sourceView = sender
popover.sourceRect = sender.bounds
presentViewController(alertController, animated: true, completion: nil)

Similar to the other responses, but this is very easy to implement in comparison.
Make your class use the UIActionSheetDelegate.
Example:
#interface ExampleViewController : UIViewController <UIActionSheetDelegate>
Then add to your ExampleViewController.mm/m
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //Get the name of the current pressed button
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Remove"]) {
NSLog(#"Remove this actionSheet"); }
if ([buttonTitle isEqualToString:#"Button 1"]) {
NSLog(#"Button 1 pressed"); }
if ([buttonTitle isEqualToString:#"Button 2"]) {
NSLog(#"Button 2 pressed"); }
if ([buttonTitle isEqualToString:#"Button 3"]) {
NSLog(#"Button 3 pressed"); }
if ([buttonTitle isEqualToString:#"Cancel"]) {
NSLog(#"Cancel clicked (anywhere away from it)"); } }
Now in a button pressed event or where/when you want this to popup call the following:
- (IBAction)aButtonPressed:(id)sender {
NSString *actionSheetTitle = #"Action Sheet"; // Title
NSString *destroyTitle = #"Destroy"; // Button titles
NSString *button1 = #"Button 1";
NSString *button2 = #"Button 2";
NSString *button3 = #"Button 3";
NSString *cancelTitle = #"Cancel";
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                              initWithTitle:actionSheetTitle
                              delegate:self
                               cancelButtonTitle:cancelTitle
                             destructiveButtonTitle:destroyTitle
                              otherButtonTitles:button1, button2, button3, nil]; [actionSheet showInView:self.view];
}
And more information about this # :
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html

Related

UIActionSheet deprecated on iOS8 [duplicate]

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"
}
}
}
}
}

How do I handle the actions of a UIAlertController?

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)
}

How to add text input in alertview of ios 8?

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)
}

How to add dynamic buttons on UIAlertController?

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..
}
})
}

How to dismiss UIAlertController when tap outside the UIAlertController?

How to dismiss UIAlertController when tap outside the UIAlertController?
I can add a UIAlertAction of style UIAlertActionStyleCancel to dismiss the UIAlertController.
But I want to add the function that when user tap outside the UIAlertController the UIAlertController will dismiss. How to do that? Thank you.
If you are targeting devices having iOS > 9.3 and using Swift and preferredStyle is Alert you can use snippet as below:
func showAlertBtnClicked(sender: UIButton) {
let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion:{
alert.view.superview?.userInteractionEnabled = true
alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
})
}
func alertControllerBackgroundTapped()
{
self.dismissViewControllerAnimated(true, completion: nil)
}
With swift 3:
func showAlertBtnClicked(sender: UIButton) {
let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .alert)
self.present(alert, animated: true) {
alert.view.superview?.isUserInteractionEnabled = true
alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
}
}
func alertControllerBackgroundTapped()
{
self.dismiss(animated: true, completion: nil)
}
Add a separate cancel action with style UIAlertActionStyleCancel. So that when user taps outside, you would get the callback.
Obj-c
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Alert Title" message:#"A Message" preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Called when user taps outside
}]];
Swift 5.0
let alertController = UIAlertController(title: "Alert Title", message: "A Message", preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {
action in
// Called when user taps outside
}))
Swift, Xcode 9
Dismiss AlertController with cancel button
provide action to your alertController where UIAlertAction's style is .cancel
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
Using this method alertController will be dismissed when user will tap to cancel action button as well as outside of the alertController.
if you don't want user to dismiss alertController after touch up outside of alertController, disable user interaction of first subviews of alertController in completion closure of present method.
self.present(alertController, animated: true) {
alertController.view.superview?.subviews[0].isUserInteractionEnabled = false
}
Dismiss AlertController on touchup outside of Controller view
If you don't want cancel button in your controller view and want to dismiss controller when user touchup outside of controller view, do so
self.present(alertController, animated: true) {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissAlertController))
alertController.view.superview?.subviews[0].addGestureRecognizer(tapGesture)
}
#objc func dismissAlertController(){
self.dismiss(animated: true, completion: nil)
}
If you are using Swift :
Add an action with addAction(_:) and style:UIAlertActionStyle.Cancel.
The `handler will be called when ou tap on the button or outside the frame.
var alertVC = UIAlertController(...) // initialize your Alert View Controller
alertVC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {
(alertAction: UIAlertAction!) in
alertVC.dismissViewControllerAnimated(true, completion: nil)
}))
Objective-C :
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:...];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[alertVC dismissViewControllerAnimated:YES completion:nil];
}]];
Swift 4:
Dismiss Action Sheet when User Taps outside Action Sheet created using UIAlertController
Code Snippet:
// Declare Action Sheet reference
var actionSheet: UIAlertController!
// Init and Show Action Sheet
func showActionSheetClicked(sender: UIButton) {
// Init Action Sheet
actionSheet = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
self.present(actionSheet, animated: true) {
// Enabling Interaction for Transperent Full Screen Overlay
self.actionSheet.view.superview?.subviews.first?.isUserInteractionEnabled = true
// Adding Tap Gesture to Overlay
self.actionSheet.view.superview?.subviews.first?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.actionSheetBackgroundTapped)))
}
}
// To dismiss Action Sheet on Tap
#objc func actionSheetBackgroundTapped() {
self.actionSheet.dismiss(animated: true, completion: nil)
}
The easiest way in Obj-C:
UIAlertController *alert = [UIAlertController alertControllerWithTitle: ...
[self presentViewController:alert animated:YES completion:^{
[alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(alertControllerBackgroundTapped)]];
}];
and then:
- (void)alertControllerBackgroundTapped
{
[self dismissViewControllerAnimated: YES
completion: nil];
}
- (void)addBackgroundDismissTapForAlert:(UIAlertController *)alert {
if (!alert.view.superview) {
return;
}
alert.view.superview.userInteractionEnabled = YES;
[alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(alertControllerBackgroundTapped)]];
for (UIView *subV in alert.view.superview.subviews) {
if (subV.width && subV.height) {
subV.userInteractionEnabled = YES;
[subV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(alertControllerBackgroundTapped)]];
}
}
}
- (void)alertControllerBackgroundTapped {
[self dismissViewControllerAnimated: YES
completion: nil];
}
UIView *alertView = self.alertController.view;
UIView *superPuperView = self.alertController.view.superview;
CGPoint tapCoord = [tap locationInView:superPuperView];
if (!CGRectContainsPoint(alertView.frame, tapCoord)) {
//dismiss alert view
}
If you view debug the superview of the alert, you see its not as simple as adding a tap gesture recognizer to the UITransitionView of the _UIAlertControllerView.You can do this instead
[presenter presentViewController:alertController animated:YES completion:^{
NSArray <UIView *>* superviewSubviews = alertController.view.superview.subviews;
for (UIView *subview in superviewSubviews) {
if (CGRectEqualToRect(subview.bounds, weakSelf.view.bounds)) {
[subview addSingleTapGestureWithTarget:weakSelf action:#selector(dismissModalTestViewController)];
}
}
}];
On iOS 15 it appears the view hierarchy for UIAlertController has changed yet again. It's presented as a new UIWindow that contains the controller itself. So in order to dismiss on tap outside:
present(alertController, animated: true) { [weak self] in
guard let self = self else { return }
let dismissGesture = UITapGestureRecognizer(target: self, action: #selector(self.shouldDismiss))
self.alertController.view.window?.isUserInteractionEnabled = true
self.alertController.view.window?.addGestureRecognizer(dismissGesture)
}
and for shouldDismiss function:
#objc private func shouldDismiss() {
alertController.dismiss(animated: true)
}
The simplest way:
- (void)viewDidLoad {
[super viewDidLoad];
[self button];
}
- (void) button {
UIButton * AlertButton = [UIButton buttonWithType:UIButtonTypeSystem];
[AlertButton setTitle:#"Button" forState:UIControlStateNormal];
AlertButton.frame = CGRectMake((self.view.frame.size.width/2) - 50 , (self.view.frame.size.height/2) - 25, 100, 50);
[AlertButton addTarget:self action:#selector(Alert) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:AlertButton];
}
- (void)Alert {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:#"Alert Title" message:#"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController: alert animated: YES completion:^{ alert.view.superview.userInteractionEnabled = YES; [alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: #selector(DismissAlertByTab)]]; }];
}
- (void)DismissAlertByTab
{
[self dismissViewControllerAnimated: YES completion: nil];
}

Resources