简体   繁体   English

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

[英]How to fire uibarbuttonitem click event programmatically

I have created a UIActionSheet我创建了一个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.UIActionSheet中的取消按钮事件中,我想触发UIBarButtonItem的事件,这在我看来。

My question is how can I fire the button event(without touching the button) in the UIActionSheet delegate method我的问题是如何在UIActionSheet委托方法中触发按钮事件(不触摸按钮)

Another way to do that and avoiding warnings is as follows:另一种避免警告的方法如下:

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

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 .然而,这会带来“未知选择器”编译器警告,但这可能会解决

@ton1n8o 's solution worked for me. @ton1n8o 的解决方案对我有用。 Here is the implementation in swift:这是swift中的实现:

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请在此处阅读原帖: performSelector 可能会导致泄漏,因为其选择器未知

For my case with RxCocoa I needed to provide a nil - object to the perform action otherwise it would crash with EXC_BAD_ACCESS :对于我使用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]; 

Well this is how I use actionSheet ..嗯,这就是我如何使用 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..完成此操作后,只需在 .m 中定义一个选择器即可。

-(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.因此,在关闭操作表中,您可以重写条形按钮项中发生的事情...希望这会有所帮助。

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: 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. actionSheet 已取消的操作表。 buttonIndex The index of the button that was clicked. buttonIndex 被点击的按钮的索引。 The button indices start at 0. If this is the cancel button index, the action sheet is canceling.按钮索引从 0 开始。如果这是取消按钮索引,则操作表正在取消。 If -1, the cancel button index is not set.如果 -1,则不设置取消按钮索引。

Discussion: This method is invoked after the animation ends and the view is hidden.讨论:该方法在动画结束并隐藏视图后调用。

actionSheet:willDismissWithButtonIndex: 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. actionSheet 即将取消的操作表。 buttonIndex The index of the button that was clicked. buttonIndex 被点击的按钮的索引。 If this is the cancel button index, the action sheet is canceling.如果这是取消按钮索引,则操作表正在取消。 If -1, the cancel button index is not set.如果 -1,则不设置取消按钮索引。

Discussion This method is invoked before the animation begins and the view is hidden.讨论该方法在动画开始之前被调用并且视图被隐藏。

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

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