How to fire uibarbuttonitem click event programmat

2019-01-13 18:55发布

问题:

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

回答1:

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.



回答2:

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

[[UIApplication sharedApplication] sendAction:barButtonItem.action
                                           to:barButtonItem.target
                                         from:nil
                                     forEvent:nil];


回答3:

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

UIApplication.sharedApplication().sendAction(barButtonItem.action, to: barButtonItem.target, from: nil, forEvent: nil)


回答4:

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]; 


回答5:

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



回答6:

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.



回答7:

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.