I have created a UIActionSheet
UIActionSheet * action = [[UIActionSheet alloc]initWithTitle:@\"\"
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];
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.
Another way to do that and avoiding warnings is as follows:
[[UIApplication sharedApplication] sendAction:barButtonItem.action
to:barButtonItem.target
from:nil
forEvent:nil];
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
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.
@ton1n8o 's solution worked for me. Here is the implementation in swift:
UIApplication.sharedApplication().sendAction(barButtonItem.action, to: barButtonItem.target, from: nil, forEvent: nil)