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

问题描述 投票:16回答:8

我创造了一个UIActionSheet

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

如果在UIActionSheet取消按钮的事件,我想发射一个UIBarButtonItem的事件,这在我看来。

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

iphone ios events uibarbuttonitem uiactionsheet
8个回答
17
投票

不知道当前的条形按钮项目操作,您可以这样调用它:

[barButtonItem.target performSelector:barButtonItem.action]

然而,这将带来“未知选择器”编译器警告,但这可能是worked around


26
投票

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

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

13
投票

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

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

2
投票

我已经阅读了接受的答案,这是非常危险的。你永远不应该压制这样的警告,以便快速通往你想要的结果!

最安全的方法:

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 may cause a leak because its selector is unknown


1
投票

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

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

1
投票

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

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

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

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


0
投票

实现委托方法

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

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

© www.soinside.com 2019 - 2024. All rights reserved.