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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9496761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:05:20  来源:igfitidea点击:

How to fire uibarbuttonitem click event programmatically

iphoneioseventsuibarbuttonitemuiactionsheet

提问by Jean Paul Scott

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 UIActionSheetI want to fire the event of a UIBarButtonItem, which is in my view.

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

My question is how can I fire the button event(without touching the button) in the UIActionSheetdelegate method

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

采纳答案by ayoy

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.

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

回答by Antonio Carlos

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

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

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

回答by Pablo

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

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

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

回答by M. Porooshani

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

回答by zero3nna

For my case with RxCocoaI needed to provide a nil - objectto 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)

回答by Ana

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

回答by Ankit Srivastava

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.

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

回答by Krrish

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.

DiscussionThis method is invoked before the animation begins and the view is hidden.

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,则不设置取消按钮索引。

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