Search code examples
ios7swiftuiactionsheetios8

UIActionSheet with swift


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?


Solution

  • You never set the action sheet's delegate:

    myActionSheet = UIActionSheet()
    myActionSheet.delegate = self