0

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.

Simran Bakshi
  • 51
  • 2
  • 7
  • 2
    Any reason you aren't using `UIAlertController`? Instead of a delegate, you would use you completion blocks specific for each action you add. `UIActionSheet` and `UIAlertView` are both deprecated in favor of `UIAlertController` anyway. – keithbhunter Jun 28 '16 at 19:30
  • I'm trying to keep my code compatible with iOS 7 and avoiding writing different code for 8 and above versions. It's not a strong reason but since the alertview has no previous version support, I'd prefer using a uiactionsheet. – Simran Bakshi Jun 28 '16 at 19:32
  • Any specific reason for supporting iOS 7? The vast majority of users are already on 9. Xcode 8/iOS 10 will drop support for iOS 7 as well if that changes your mind any: [see this link](https://twitter.com/jamesthomson/status/747451011183280128) – keithbhunter Jun 28 '16 at 19:35
  • Okay, point noted. But do you still think there is something faulty with my implementation? Also, will the emulator still run this code? – Simran Bakshi Jun 28 '16 at 19:38
  • My code base is largely iOS7 supported as of now. It'll be a massive revamp which will come a little later. I'm trying to find a work around this for the time being. – Simran Bakshi Jun 28 '16 at 19:39
  • You probably need to provide more context. I am currently assuming that whatever `self` is, gets deallocated before the user taps on any of the buttons. The delegate is then nil and nothing will happen. – Bringo Jun 29 '16 at 01:09
  • Self is my object class which gets instantiated inside the view controller. The view is being passed from the view controller to this class as a parameter for `sheet.showInView(self.view)` – Simran Bakshi Jun 29 '16 at 05:22

2 Answers2

1

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.

Peterdk
  • 15,625
  • 20
  • 101
  • 140
0

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

Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25