This question already has answers here:
Passing parameters to addTarget:action:forControlEvents

(13个回答)


6年前关闭。




我是iOS星球的新手,下面是我的示例代码:
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIButton  * _btnSample = [UIButton buttonWithType:UIButtonTypeCustom];

    [_btnSample setFrame: CGRectMake(100, 160, 200, 30)];

    [_btnSample setBackgroundColor:[UIColor redColor]];

    [_btnSample setTitle:@"Click Me" forState:UIControlStateNormal];

    [_btnSample addTarget:self action:@selector(btnClick::)withObjects   forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_btnSample];

    //[self btnClick:@"12" :@"13"];

}


- (void)btnClick :(NSString *) stringValue1 : (NSString *) stringValue2
{
    NSLog(@"Click ME Button Clicked with a value::%@",stringValue1);
       NSLog(@"Click ME Button Clicked with a value::%@",stringValue2);
}

@selector中,我需要调用btnClick fn,它有两个参数,怎么办?

最佳答案

简短的答案是你不能。控件调用的action方法必须符合以下签名之一(请参阅Apple reference):

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

这是因为操作方法的参数由控件提供,并提供给操作方法以决定必要的操作。

如果您能解释这些参数的含义(正如我从您的问题中看不到的那样),那么我们几乎可以确定您的问题的解决方案。

10-06 13:25
查看更多