0

This a rookie question.. I created the UIActionSheet. Now how do i get it perform "delete" when its pressed.

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.subtype == UIEventSubtypeMotionShake )
    {
        // Handle shake notification
    UIActionSheet *shakeActionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                  delegate:self
                                                         cancelButtonTitle:@"Cancel"
                                                    destructiveButtonTitle:@"Delete"
                                                         otherButtonTitles:nil];

    [shakeActionSheet showInView:self];
    }

    if ([super respondsToSelector:@selector(motionEnded:withEvent:)])
        [super motionEnded:motion withEvent:event];
}
user1779598
  • 51
  • 1
  • 10

3 Answers3

2

try to call UIActionSheet delegate methods

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{

    NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Delete"])
    {
     //do your stuff in here

     }

}
SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
  • Thanks! ok i also want the action sheet to disappear and do nothing if the user decides to touch the screen instead of the cancel button. how do i implement that? – user1779598 Feb 01 '13 at 18:33
  • 1
    you need to use `UITapGestureRecognizer` http://stackoverflow.com/questions/2623417/iphone-sdk-dismissing-modal-viewcontrollers-on-ipad-by-clicking-outside-of-it – SpaceDust__ Feb 01 '13 at 18:40
  • That's a new question. You should mark one of these answers correct. And tap gesture recognizer is also a good answer. – danh Feb 01 '13 at 18:41
  • I didn't know you could do that. how do i "mark one of these answers correct"? @danh – user1779598 Feb 01 '13 at 18:43
  • There's a little checkbox near the votes for the answer. Simply click it on the answer that helped you the most. – danh Feb 01 '13 at 18:44
  • It's far from ideal to perform actions based on the button title. Using the button index is much safer. Comparing the title will fail if you change the button's title (but forget to update this code) or if your app supports more than one language. – rmaddy Feb 01 '13 at 19:02
  • @SpaceDust i tried your suggestion. However its not working for me. i posted a new question about it and would appreciate your help http://stackoverflow.com/questions/14657919/get-uiactionsheet-to-dismiss-by-pressing-anywhere-on-screen – user1779598 Feb 02 '13 at 05:26
  • @user1779598 you can try change `if([title isEqualToString:@"Delete"])` to `if(buttonIndex==integer){}` its not big deal. – SpaceDust__ Feb 02 '13 at 19:33
1

Your class has to conform to the UIActionSheetDelegate protocol.

Then override the actionSheet:clickedButtonAtIndex: method, evaluate the clicked button and act accordingly.

See here for more info.

SAE
  • 1,609
  • 2
  • 18
  • 22
0

Just to add on to other two solutions, you can add the delegate in your - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event method like this (assuming your class implements UIActionSheetDelegate protocol):

shakeActionSheet.delegate = self;
AC1
  • 463
  • 3
  • 9