Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 416
moteb Avatar asked Jun 11 '14 21:06

moteb


People also ask

What is Uiactionsheet in Swift?

A view that presents a set of alternatives for how to proceed with a task.

How do I add an image to an ActionSheet in Swift?

To add an image in an action sheet you need to do something like this... UIAlertAction* action = [UIAlertAction actionWithTitle:@"Action 1" style:UIAlertActionStyleDefault handler:nil]; [action setValue:[[UIImage imageNamed:@"myImage. png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];


1 Answers

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

 }
like image 199
Anit Kumar Avatar answered Nov 18 '22 07:11

Anit Kumar