Greg S
Greg S

Reputation: 21

ActionSheet Actions in Swift

I am new to swift and IOS development in general so I am made an Action Sheet in Xcode, but now I want to perform actions from the Action Sheet. I am familiar with doing this in ObjC by using a button Index, but I can not seem to figure this out in swift.

I would also like to customize my Action sheet to make the text different colors, but that is not as important. Please let me know if you can help. Thanks

Here is my code so far but I the .tag part of the Action when the button is pressed is wrong...

    @IBAction func ActionSheet(sender: UIButton) {

    var sheet: UIActionSheet = UIActionSheet();
    let title: String = "Action Sheet!";
    sheet.title  = title;
    sheet.addButtonWithTitle("Cancel");
    sheet.addButtonWithTitle("A course");
    sheet.addButtonWithTitle("B course");
    sheet.addButtonWithTitle("C course");
    sheet.cancelButtonIndex = 0;
    sheet.showInView(self.view);

}


func actionSheet(sheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) {
    if (actionSheet.tag == 1) {
        NameLabel.text = "I am confused"

    }
}

Upvotes: 0

Views: 2234

Answers (3)

Srujan Simha Adicharla
Srujan Simha Adicharla

Reputation: 3723

I created a library that you can make it work like an action sheet view :)

https://github.com/srujanadicharla/SASelectionView

Upvotes: 0

vacawama
vacawama

Reputation: 154631

You need to:

  1. Make your view controller conform to the UIActionSheetDelegate protocol:

    class ViewController: UIViewController, UIActionSheetDelegate {

  2. Add self as the delegate, and set the tag to 1:

    var sheet: UIActionSheet = UIActionSheet()
    let title: String = "Action Sheet!"
    sheet.title  = title
    sheet.addButtonWithTitle("Cancel")
    sheet.addButtonWithTitle("A course")
    sheet.addButtonWithTitle("B course")
    sheet.addButtonWithTitle("C course")
    sheet.cancelButtonIndex = 0
    sheet.delegate = self                  // new line here
    sheet.tag = 1                          // another new line here
    sheet.showInView(self.view)
    
  3. Now you can use the buttonIndex:


func actionSheet(sheet: UIActionSheet!, clickedButtonAtIndex buttonIndex: Int) {
    if sheet.tag == 1 {
        println(buttonIndex)
        println(sheet.buttonTitleAtIndex(buttonIndex))
    }
}

Upvotes: 1

bllakjakk
bllakjakk

Reputation: 5065

I think your sequence is not correct, also the delegate is not set.

Declare Your class as delegate

class ViewController: UIViewController, UIActionSheetDelegate{

...

Define Action Sheet

  @IBAction func ActionSheet(sender: UIButton) {

    let actionSheet = UIActionSheet(title: "ActionSheet", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Done", otherButtonTitles: "A Course", "B Course", "C course")
    actionSheet.showInView(self.view)

}

Define Delegate Function

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

    case 0:
        NSLog("Done");
        break;
    case 1:
        NSLog("Cancel");
        break;
    case 2:
        NSLog("A Course");
        break;
    case 3:
        NSLog("B Course");
        break;
    case 4:
        NSLog("C Course");
        break;

 }

Upvotes: 0

Related Questions