How can I send two arguments in a selector method?
In the following, how can I send two arguments in togBut开发者_JAVA百科ton
method? I tried, but it did not work.
[button addTarget:self action:@selector(togButton:)
forControlEvents:UIControlEventTouchUpInside];
control targets only accept one argument for their actions: the control that's being manipulated. There's no way to do what you want (the OS just doesn't support it).
What are you trying to put in there? Maybe you can use the tag
property, or if that's not sufficient, subclass UIButton and add instance variables that you can then access in -togButton:
.
This is an example of a work around to get data from UI component
-(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";
}
now you have cell and indexPath and you can do what you want. I hope I helped.
Also you can use structs to wrap your arguments in one parameter.
The best way to send two arguments to the method is to wrap them in a class. For example, if the two are arguments are strings then you would have a class solely comprised of string iVars.
The action selector called by a button tap can have zero, one, or two parameters. The first is the sender (the button that was tapped) and the second is the event (the tap). See the target-action mechanism for controls:
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event
As Thomas Müller suggests, the best way to pass app-specific data to your action selector is by subclassing UIButton (or, in the case of a button on a UITableViewCell, you can add the app-specific data to a UITableViewCell subclass and access the cell as the button's superview.superview).
[Class instancesRespondToSelector: @selector (setTo:over:)]
I remember seeing this Objective-C code in a book.
精彩评论