Simran Bakshi
Simran Bakshi

Reputation: 51

UIActionsheet delegate clickedButtonAtIndex never gets called

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.

Upvotes: 0

Views: 483

Answers (2)

Peterdk
Peterdk

Reputation: 16015

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.

Upvotes: 1

Bhadresh Mulsaniya
Bhadresh Mulsaniya

Reputation: 2640

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

Upvotes: 0

Related Questions