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

时间:2022-11-28 21:28:49

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委托方法中触发按钮事件(不触摸按钮)

7 个解决方案

#1


14  

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.

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

#2


23  

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

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

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

#3


9  

@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)

#4


2  

You can use this method to fire the tap event programmatically for a specific button:

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

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

#5


2  

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

#6


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.

所以在解雇行动表内你可以重新写下栏内按钮项目内发生的事情......希望这会有所帮助。

#7


0  

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

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

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.

actionSheet被解雇的操作表。 buttonIndex单击的按钮的索引。按钮索引从0开始。如果这是取消按钮索引,则操作表正在取消。如果为-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

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex参数

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.

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

Discussion This method is invoked before the animation begins and the view is hidden.

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

#1


14  

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.

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

#2


23  

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

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

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

#3


9  

@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)

#4


2  

You can use this method to fire the tap event programmatically for a specific button:

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

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

#5


2  

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

#6


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.

所以在解雇行动表内你可以重新写下栏内按钮项目内发生的事情......希望这会有所帮助。

#7


0  

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

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

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.

actionSheet被解雇的操作表。 buttonIndex单击的按钮的索引。按钮索引从0开始。如果这是取消按钮索引,则操作表正在取消。如果为-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

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex参数

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.

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

Discussion This method is invoked before the animation begins and the view is hidden.

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