0

I have 3 buttons in my UIActionsheet. I want one cancel button at the bottom of it.

I also want that all of the buttons should be able to dismiss UIActionsheet.

Currently, none of them does the job for me.

Here is how I display it:

UIActionSheet *actionSheet = nil;
NSArray *otherButtons = nil;


otherButtons = [NSArray arrayWithObjects:@"Button1", @"Button2", @"Button3",
                     nil];       

actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                          delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];


for( NSString *btntitle in otherButtons)
{
    [actionSheet addButtonWithTitle:btntitle];
}    

[actionSheet addButtonWithTitle:@"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;

actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];

In my delegate, I do this:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
      //some code
      [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
}

But it does not dismiss. I do not want to change my design and position of any buttons. What should I do?

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • 1
    Got how to make the cancel button work: http://stackoverflow.com/questions/8550579/uiactionsheet-cancel-button-not-dismissing-actionsheet. However other buttons still do not discard the sheet. – Nirav Bhatt Dec 01 '12 at 09:55
  • you did correctly. I am not sure where is the problem. but try only dismiss your actionsheet in - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex method. comment all other code inside that method. check whether it is working or nor. – SARANGA Dec 01 '12 at 09:58
  • I think you mentioned that you are doing some performance in - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex method. before dismiss. So avoid that. – SARANGA Dec 01 '12 at 10:02
  • if you want to do some operation while dismiss means do in a seperate function and call that function with "perform selector withdelay" method – SARANGA Dec 01 '12 at 10:04
  • it seems to be working but on second click, not the first. I did not understand your second comment. Do you mean the actual operation and dismissing should be in separate call? Also could you paste some code that will work irrespective of buttonIndex? because I need it on all buttons, including cancel. – Nirav Bhatt Dec 01 '12 at 10:06
  • hey, do you have tab bar in this view?? – SARANGA Dec 01 '12 at 11:06
  • if you have any tab bar under the view means, try this, showFromTabBar: – SARANGA Dec 01 '12 at 11:08
  • I was having the same problem. My action sheet was in a view controller inside a navigation controller inside a tab bar controller. Following your link (and as SARANGA noted) I did as follows: `[actionSheet showInView:self.tabBarController.view]`. It seems to have solved the problem. Also, note that you shouldn't really need to dismiss explicitly. You should be able to delete `[actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];`. According to the documentation on `actionSheet:clickedButtonAtIndex`, "The receiver is automatically dismissed after this method is invoked." – salo.dm Dec 01 '12 at 11:31
  • Tried both: [actionSheet showFromTabBar:self.self.tabBarController.tabbar] and [actionSheet showInView:self.tabBarController.view]. No success. – Nirav Bhatt Dec 01 '12 at 12:19
  • Tab bar seems to be the pointer though, because in other View controller of my app where there is no tab bar, an action sheet works just as expected without dismissing call. – Nirav Bhatt Dec 01 '12 at 12:22
  • What I observe here is that on first tap it focuses on the action sheet. On second tap it performs the button tap. – Nirav Bhatt Dec 01 '12 at 13:22
  • Which brings me to the conclusion: since I launch it on long press, it was launching twice!!! I just put a bool flag to know if it's launched already, and if yes, don't launch it again. Worked like charm! Thanks for the important pointers along the way! Dismissing wasn't required. – Nirav Bhatt Dec 01 '12 at 13:32

5 Answers5

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

    [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
    [self performSelector:@selector(OtherOperations) withObject:nil afterDelay:0.0];

}

-(void)OtherOperations{      //some code
}
Senthilkumar
  • 2,471
  • 4
  • 30
  • 50
SARANGA
  • 172
  • 1
  • 8
  • I've a little concern here, what is going here that first of all you're dismissing and then executing a selector...really a nice hack but we really don't know what important stuff Nirav performed here so the case may be he wants the dismissing later if his task goes successfully and also in time of interruption we're not sure that selector will perform his task completely or not that why I suggest to start a new thread as it allocate from kernel, so distinct process... – Mohit_Jaiswal Dec 01 '12 at 10:30
  • It doesn't matter if sheet remains while operation is on or not. So this design is OK - however, it doesn't still serve any purpose. I still have to tap twice to dismiss it - first tap doesn't do the job. – Nirav Bhatt Dec 01 '12 at 10:45
1

Be careful when opening an action sheet from a lpgr. Only open it on "Began", not again on "Ended".

- (void) handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:self.tblYourVids];

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSIndexPath *indexPath = [self.aTable indexPathForRowAtPoint:p];
        if (indexPath == nil)
            NSLog(@"long press on table view but not on a row");
        else {
            NSLog(@"long press on table view at row %d", indexPath.row);
            self.selectedCell = [self.aTable cellForRowAtIndexPath:indexPath];
            DLog(@"open action sheet only once");
            [self showActionSheet];
        }
    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // eat this one
    }
}
Jimmy_m
  • 1,568
  • 20
  • 24
0

you have to add this to yours .h file.

 <UIActionSheetDelegate>

Also add this line to yours .m file.

 actionSheet.delegate = self;

Let me know, this is working or not. If works, accept it as right answer

Ajay Chaudhary
  • 1,993
  • 15
  • 23
  • Ajay, Nirav is already assigning the delegate at the time when he is making the object of actionSheet, have you checked that so there's again no need of: actionSheet.delegate = self; – Mohit_Jaiswal Dec 01 '12 at 10:04
-1

just try to use the below code.it will automatically dismiss.

UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@""
                                                       delegate:self
                                              cancelButtonTitle:@"cancel"
                                         destructiveButtonTitle:@"Printer"
                                              otherButtonTitles: nil];

    [sheet showInView:self.view];
    [sheet release];


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

let me know whether it is working or not

Happy Coding!!!!!

NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • This doesn't do anything except putting an extra button. Try to resolve by code instead of suggesting design changes. – Nirav Bhatt Dec 01 '12 at 10:15
-1

This is not the right way to cancel UIActionSheet firstly you have to disable to default UIActionSheet by javascipt and go through this web

http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/