Question

This is my first post on stackoverflow, so please forgive me if I make some mistakes.

I'm a relatively new app designer, and I have been working on a new app that I'm hoping to publish soon. I have recently run into an issue. I am not able to use UIActionSheet. While it displays fine, the buttons simply make the action sheet disappear. I have tried many things to no avail; I did everything that was suggested in this thread (made by someone with a similar but not identical problem). I have tested and made sure that the function is not called altogether by using NSLog. I have tried changing the showInView, although it doesn't seem like that should make a difference anyway. So anyway, here's my current code:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if ( event.subtype == UIEventSubtypeMotionShake )
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"You Shook?" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"End Game" otherButtonTitles:@"Settings", nil];
        [actionSheet showInView:self.view];
    }

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
        [super motionEnded:motion withEvent:event];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        UIAlertView *endGameAlertView = [[UIAlertView alloc] initWithTitle:@"End Game" message:@"Are you sure you want to end the current game?" delegate:nil cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
        [endGameAlertView show];

    }

    else
    {
        UIAlertView *unimplementedSettingsAlertView = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"This feature is not yet implemented" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [unimplementedSettingsAlertView show];
    }
}

As you can see, the actionSheet is triggered by shaking the device (which works perfectly), again, the only issue is that the buttons do nothing except for close the actionSheet. I'm using Xcode 5.0.2 on OSX 10.9.1. Let me know if I left out any important information. Thank you in advance!

~Junior

Was it helpful?

Solution

Currently you are setting the actionSheet's delegate to nil because of which your delegate method is not getting called.

In your ViewController.h file, write like this:

@interface ViewController : UIViewController <UIActionSheetDelegate>

You have to set the delegate of your actionSheet in order to get the delegate method called.

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"You Shook?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"End Game" otherButtonTitles:@"Settings", nil];

OTHER TIPS

set the delegate to self

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"You Shook?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"End Game" otherButtonTitles:@"Settings", nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top