UIActionsheet delegate clickedButtonAtIndex never gets called

473 Views Asked by At

I am trying to create a function which creates an actionsheet when called from different viewcontrollers. The issue is that although the uiaction sheet gets created, I can't seem to perform any of the designated click actions. Infact the delegate never gets called. On clicking any of the buttons the actionsheet is dismissed.

My code for the function is:

func shareAction(){
    var sheet: UIActionSheet = UIActionSheet();
    print("Calling share action!");
    let title: String = "Tell friends, make plans";

    sheet.title  = title;
    sheet.addButtonWithTitle("Cancel")
    sheet.addButtonWithTitle("WhatsApp");
    sheet.addButtonWithTitle("Facebook");
    sheet.addButtonWithTitle("Message");
    sheet.addButtonWithTitle("More");
    sheet.cancelButtonIndex = 0;
    sheet.delegate=self;
    sheet.showInView(self.view)

}

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
{
    print("clicking the action sheet")
    if(buttonIndex ==0){
         print("this is 0")
}

This is inside a class which extends NSObject and UIActionSheetDelegate. I have gone through every bit of documentation and am stumped as to what is causing the issue.

2

There are 2 best solutions below

0
Bhadresh Mulsaniya On

Create your function like such a way that it accept one argument of type id. In which you have to pass your current viewcontroller object(self).

0
Peterdk On

I had somewhat the same issue. Problem was that the generic class that handled the ActionSheet actions and was the delegate was deallocated by ARC. So the delegate self is deallocated, and won't be called.

Make sure you keep the class that implements the ActionSheet calls around. For example you could save it into a @property variable in the calling viewcontroller.