19

I have created a UIActionSheet

UIActionSheet * action = [[UIActionSheet alloc]initWithTitle:@""
                                                              delegate:self
                                                     cancelButtonTitle: @"cancel"
                                                destructiveButtonTitle: @"OK"
                                                     otherButtonTitles: nil];
          [action showInView:self.view];
          [action release];

On the event of the cancel button in the UIActionSheet I want to fire the event of a UIBarButtonItem, which is in my view.

My question is how can I fire the button event(without touching the button) in the UIActionSheet delegate method

Jean Paul Scott
  • 2,229
  • 4
  • 25
  • 37
  • which method you are calling when User tap UIBarButtonItem? – Rupesh Feb 29 '12 at 09:55
  • I am in a messy situation. I have a `UIButton` over this `UIBarButtonItem`. On the click of `UIButton`, I load a `UIActionSheet`. And on the click of the cancel button in `UIActionSheet`, I need to fire the event of `UIBarButtonItem`. – Jean Paul Scott Feb 29 '12 at 10:07
  • Paul i could not understand that why you make `UIBarButtonItem` if your `UIBarButtonItem` is under the `UIButton`? – hchouhan02 Feb 29 '12 at 10:20

8 Answers8

28

Another way to do that and avoiding warnings is as follows:

[[UIApplication sharedApplication] sendAction:barButtonItem.action
                                           to:barButtonItem.target
                                         from:nil
                                     forEvent:nil];
Antonio Carlos
  • 758
  • 9
  • 18
18

Not knowing the current bar button item action you can invoke it this way:

[barButtonItem.target performSelector:barButtonItem.action]

This will however bring "unknown selector" compiler warning, but this may be worked around.

Community
  • 1
  • 1
ayoy
  • 3,755
  • 17
  • 20
14

@ton1n8o 's solution worked for me. Here is the implementation in swift:

UIApplication.sharedApplication().sendAction(barButtonItem.action, to: barButtonItem.target, from: nil, forEvent: nil)
Pablo
  • 273
  • 2
  • 8
2

I've read the accepted answer and it is awfully DANGEROUS. you should never suppress such warnings in order to shortcut the path to your desired result!

the safest way to do so:

SEL selector=barButton.action;
id target=barButton.target;
  if(selector && target){
     IMP imp = [target methodForSelector:selector];
     void (*func)(id, SEL) = (void *)imp;
     func(target, selector);
  }

Please read the original post here: performSelector may cause a leak because its selector is unknown

Community
  • 1
  • 1
M. Porooshani
  • 1,737
  • 5
  • 32
  • 39
2

For my case with RxCocoa I needed to provide a nil - object to the perform action otherwise it would crash with EXC_BAD_ACCESS:

let button = sut.navigationItem.leftBarButtonItem!
_ = button.target?.perform(button.action, with: nil)
zero3nna
  • 2,524
  • 28
  • 28
1

You can use this method to fire the tap event programmatically for a specific button:

[self performSelector:@selector(buttonClicked:) withObject:self.myButton afterDelay:0.0]; 
Ana
  • 583
  • 1
  • 4
  • 11
0

Well this is how I use actionSheet ..

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

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
[pickerView release];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];

[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

Once you are done with this just define a selector in your .m like..

-(void)dismissActionSheet{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
}

so inside dismiss action sheet you can re-write what is happening inside bar button item... hope this helps.

Ankit Srivastava
  • 11,975
  • 9
  • 57
  • 111
0

Implement the delegate method

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

OR

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == actionSheet.destructiveButtonIndex)
    {

    }
    else if(buttonIndex == (actionSheet.cancelButtonIndex))
    {
        // Call your event here
        // Fire the event of UIBarButtonItem.
    }

}

actionSheet:didDismissWithButtonIndex:

Sent to the delegate after an action sheet is dismissed from the screen.

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex Parameters

actionSheet The action sheet that was dismissed. buttonIndex The index of the button that was clicked. The button indices start at 0. If this is the cancel button index, the action sheet is canceling. If -1, the cancel button index is not set.

Discussion: This method is invoked after the animation ends and the view is hidden.

actionSheet:willDismissWithButtonIndex:

Sent to the delegate before an action sheet is dismissed.

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex Parameters

actionSheet The action sheet that is about to be dismissed. buttonIndex The index of the button that was clicked. If this is the cancel button index, the action sheet is canceling. If -1, the cancel button index is not set.

Discussion This method is invoked before the animation begins and the view is hidden.

Krrish
  • 2,256
  • 16
  • 21