如何以编程方式触发uibarbuttonitem点击事件

我已经创build了UIActionSheet

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

UIActionSheet取消button的事件我想要触发一个UIBarButtonItem事件,这是在我看来。

我的问题是我怎样才能触发UIActionSheet委托方法中的button事件(无需触摸button)

不知道当前的酒吧button项目动作,你可以这样调用它:

 [barButtonItem.target performSelector:barButtonItem.action] 

这将会带来“未知select器”的编译器警告,但是这可能会被解决 。

另一种方法做到这一点,避免警告如下:

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

@ ton1n8o的解决scheme为我工作。 这里是swift的实现:

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

您可以使用此方法以编程方式为特定button触发轻敲事件:

 [self performSelector:@selector(buttonClicked:) withObject:self.myButton afterDelay:0.0]; 

我已经阅读了接受的答案,这是非常危险的。 你不应该压制这样的警告,以便捷径到你想要的结果!

最安全的方法是:

 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); } 

请在这里阅读原帖: performSelector可能会导致泄漏,因为它的select器是未知的

那么这是我如何使用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)]; 

一旦你完成这个只是在你的.m中定义一个select器..

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

所以里面的解雇行动表,你可以重新写在棒button项目内发生了什么…希望这有助于。

实现委托方法

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

要么

 - (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:

操作表从屏幕上消失后发送给委托人。

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

actionSheet被解雇的行动表。 buttonIndex被点击的button的索引。 button索引从0开始。如果这是取消button索引,则操作表正在取消。 如果-1,取消button索引没有设置。

讨论:在animation结束并隐藏视图之后调用此方法。

actionSheet:willDismissWithButtonIndex:

在行动表被解散之前发送给代表。

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

actionSheet即将被解雇的行动表。 buttonIndex被点击的button的索引。 如果这是取消button索引,则操作表正在取消。 如果-1,取消button索引没有设置。

讨论在animation开始之前调用此方法,并隐藏视图。