【发布时间】:2010-12-23 21:21:15
【问题描述】:
在下面,如何在togButton 方法中发送两个参数?我试过了,还是不行。
[button addTarget:self action:@selector(togButton:)
forControlEvents:UIControlEventTouchUpInside];
【问题讨论】:
标签: iphone objective-c cocoa-touch uikit
在下面,如何在togButton 方法中发送两个参数?我试过了,还是不行。
[button addTarget:self action:@selector(togButton:)
forControlEvents:UIControlEventTouchUpInside];
【问题讨论】:
标签: iphone objective-c cocoa-touch uikit
控制目标只接受其动作的一个参数:被操纵的控制。没有办法做你想做的事(操作系统不支持它)。
【讨论】:
你想在里面放什么?也许您可以使用 tag 属性,或者如果这还不够,则将 UIButton 子类化并添加实例变量,然后您可以在 -togButton: 中访问这些变量。
【讨论】:
这是一个从 UI 组件获取数据的解决方法示例
-(void) switchChanged:(id) sender
{
UISwitch* switchControl = (UISwitch*)sender;
UITableViewCell *cell = (UITableViewCell*)[sender superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
cell.textLabel.text =" change text title";
}
现在你有了 cell 和 indexPath,你可以做你想做的事了。我希望我能帮上忙。
您还可以使用结构将参数包装在一个参数中。
【讨论】:
向方法发送两个参数的最佳方法是将它们包装在一个类中。例如,如果这两个参数都是字符串,那么您将拥有一个仅由字符串 iVars 组成的类。
【讨论】:
按钮点击调用的动作选择器可以有零个、一个或两个参数。第一个是发送者(被点击的按钮),第二个是事件(点击)。 有关控件,请参见 target-action mechanism:
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event
正如 Thomas Müller 所建议的,将特定于应用程序的数据传递给您的操作选择器的最佳方式是通过子类化 UIButton(或者,对于 UITableViewCell 上的按钮,您可以将特定于应用程序的数据添加到 UITableViewCell 子类并将单元格作为按钮的 superview.superview 访问)。
【讨论】:
[Class instancesRespondToSelector: @selector (setTo:over:)]
我记得在一本书中看到过这段 Objective-C 代码。
【讨论】:
setToOver 方法需要两个整数,您答案中的代码基本上等同于try {Class<?>[] types={int.class, int.class}; MyClass.class.getMethod("setToOver", types); hasMethod=true; } catch (NoSuchMethodException e) {hasMethod=false;}。它只是告诉你一个类是否有方法;它不调用该方法。有关getMethod 和反射的更多信息,请参阅java.sun.com/docs/books/tutorial/reflect。