Bha*_*odi 0 iphone cocoa objective-c ibaction
我正在以编程方式访问IBAction并希望通过此IBAction调用传递两个参数.任何人都可以轻松推荐......
IBActions通常由用户界面元素调用,并且它们不能具有任意数量的参数.
如果要以编程方式调用action方法,可以通过将字典作为参数传递来滥用sender参数,并保存要传递的实际参数,如下所示:
- (void) foo
{
[self myAction: [NSDictionary dictionaryWithObject: @"bar" forKey: @"baz"]];
}
Run Code Online (Sandbox Code Playgroud)
但是,我建议使用两个参数创建一个额外的方法; IBAction可以使用适合发送方的参数调用它,并且可以通过编程方式使用您需要的任何参数调用它.这可能是代码的可能大纲:
// The atual "logic" method, doing sth interesting
- (void) foo: (NSString *) s bar: (NSInteger) i
{
// some code
}
- (IBAction) myAction: (id) sender
{
// can be invoked by a button, or any view action
if (sender == self.buttonX) {
[self foo: @"x" bar: 42];
}
if (sender == self.buttonY) {
[self foo: @"y" bar: 4];
}
}
- (void) methodCallingFooBarProgrammatically
{
[self foo: @"s" bar: 17];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4620 次 |
| 最近记录: |