繁体   English   中英

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

[英]How to fire uibarbuttonitem click event programmatically

我创建了一个UIActionSheet

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

UIActionSheet中的取消按钮事件中,我想触发UIBarButtonItem的事件,这在我看来。

我的问题是如何在UIActionSheet委托方法中触发按钮事件(不触摸按钮)

另一种避免警告的方法如下:

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

不知道当前的栏按钮项目操作,您可以通过以下方式调用它:

[barButtonItem.target performSelector:barButtonItem.action]

然而,这会带来“未知选择器”编译器警告,但这可能会解决

@ton1n8o 的解决方案对我有用。 这是swift中的实现:

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

我已经阅读了接受的答案,这是非常危险的。 您永远不应该抑制此类警告,以便为您提供所需结果的捷径!

最安全的方法是:

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 可能会导致泄漏,因为其选择器未知

对于我使用RxCocoa 的情况,我需要为执行操作提供一个nil - object ,否则它会因EXC_BAD_ACCESS崩溃:

let button = sut.navigationItem.leftBarButtonItem!
_ = button.target?.perform(button.action, with: nil)

您可以使用此方法以编程方式为特定按钮触发点击事件:

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

嗯,这就是我如何使用 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 中定义一个选择器即可。

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

因此,在关闭操作表中,您可以重写条形按钮项中发生的事情...希望这会有所帮助。

实现委托方法

- (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 被点击的按钮的索引。 按钮索引从 0 开始。如果这是取消按钮索引,则操作表正在取消。 如果 -1,则不设置取消按钮索引。

讨论:该方法在动画结束并隐藏视图后调用。

actionSheet:willDismissWithButtonIndex:

在取消操作表之前发送给委托人。

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

actionSheet 即将取消的操作表。 buttonIndex 被点击的按钮的索引。 如果这是取消按钮索引,则操作表正在取消。 如果 -1,则不设置取消按钮索引。

讨论该方法在动画开始之前被调用并且视图被隐藏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM