10

I created an action sheet, but the problem is that the delegate method is not called

 myActionSheet = UIActionSheet()
        myActionSheet.addButtonWithTitle("Add event")
        myActionSheet.addButtonWithTitle("close")
        myActionSheet.cancelButtonIndex = 1
        myActionSheet.showInView(self.view)

/// UIActionSheetDelegate

func actionSheet(myActionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int){
        if(myActionSheet.tag == 1){

            if (buttonIndex == 0){
                println("the index is 0")
            }
        }
}

I used another way which worked good with iOS 8 but did not work with iOS 7:

var ActionSheet =  UIAlertController(title: "Add View", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)

ActionSheet.addAction(UIAlertAction(title: "Add event", style: UIAlertActionStyle.Default, handler:nil))

self.presentViewController(ActionSheet, animated: true, completion: nil)

Any idea to solve the problem?

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
moteb
  • 113
  • 1
  • 1
  • 4
  • 4
    action sheet and alert view are depreciated in iOS 8 and alertController is introduced. – nikhil84 Jun 23 '14 at 11:40
  • 3
    Yeah its deprecated but if you support your app for iOS 7 then alertController will not work. So better check the iOS version and call the appropriate code for iOS 8 and iOS 7 – Mahmud Ahsan May 03 '15 at 03:15
  • Have a look on this answer - http://stackoverflow.com/questions/27787777/how-to-create-uiactionsheet-actions/27798750#27798750 – Kampai Jul 02 '15 at 07:14

5 Answers5

20

UIActionSheet in swift language :-

Action Sheet with cancelButton and destructiveButton

set the UIActionSheetDelegate.

        let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done")
        actionSheet.showInView(self.view)

Action Sheet with cancelButton , destructiveButton and otherButton

        let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "Yes", "No")
        actionSheet.showInView(self.view)

create the Action sheet function

func actionSheet(actionSheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int)
{
    switch buttonIndex{

    case 0:
        NSLog("Done");
        break;
    case 1:
        NSLog("Cancel");
        break;
    case 2:
        NSLog("Yes");
        break;
    case 3:
        NSLog("No");
        break;
    default:
        NSLog("Default");
        break;
        //Some code here..

 }
Anit Kumar
  • 8,075
  • 1
  • 24
  • 27
  • I wrote up a Swift version that doesn't rely on hardcoded button indexes for another answer: http://stackoverflow.com/a/29272140/37168 – stone Mar 27 '15 at 23:22
14

UIActionSheet is deprecated since iOS8, I would recommend using UIAlertController if you don't have to support version below:

private func presentSettingsActionSheet() {
  let settingsActionSheet: UIAlertController = UIAlertController(title:nil, message:nil, preferredStyle:UIAlertControllerStyle.ActionSheet)
  settingsActionSheet.addAction(UIAlertAction(title:"Send Feedback", style:UIAlertActionStyle.Default, handler:{ action in
    self.presentFeedbackForm()
  }))
  settingsActionSheet.addAction(UIAlertAction(title:"Tell Me a Joke!", style:UIAlertActionStyle.Default, handler:{ action in
    self.presentRandomJoke()
  }))
  settingsActionSheet.addAction(UIAlertAction(title:"Cancel", style:UIAlertActionStyle.Cancel, handler:nil))
  presentViewController(settingsActionSheet, animated:true, completion:nil)
}

Here is what it looks like presented:

                                     AlertViewController in Swift

Zorayr
  • 23,770
  • 8
  • 136
  • 129
8

You never set the action sheet's delegate:

myActionSheet = UIActionSheet()
myActionSheet.delegate = self
Ian Henry
  • 22,255
  • 4
  • 50
  • 61
0

Updated for Swift 3 :

If you want to show/open UIActionSheet on Button click, used below simple and updated code in yourViewController:

Method definition:

func showPaymentModeActionSheet()  {
    
    // 1
    let optionMenu = UIAlertController(title: nil, message: "Choose Payment Mode", preferredStyle: .actionSheet)
    
    // 2
    let fullAction = UIAlertAction(title: "FULL", style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        self.mPaymentModeTextField.text = "FULL"
        
    })
    let advanceAction = UIAlertAction(title: "ADVANCE", style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        self.mPaymentModeTextField.text = "ADVANCE"
    })
    
    //
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
        (alert: UIAlertAction!) -> Void in
    })
    
    // 4
    optionMenu.addAction(fullAction)
    optionMenu.addAction(advanceAction)
    optionMenu.addAction(cancelAction)
    
    // 5
    self.present(optionMenu, animated: true, completion: nil)
}

Method call:

@IBAction func actionOnPaymentModeButton(_ sender: Any) {
    // open action sheet
    showPaymentModeActionSheet()
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kiran Jadhav
  • 3,209
  • 26
  • 29
0

Show Action sheet in Swift 4 / 5

    @IBAction func showActionSheet(sender: AnyObject) {

    let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet)

    
    alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in
        print("User click Edit button")
    }))

    alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in
        print("User click Delete button")
    }))
    
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction)in
        print("User click Dismiss button")
    }))


    self.present(alert, animated: true, completion: {
        print("completion block")
    })
}

enter image description here

Solayman Rana
  • 283
  • 3
  • 12