How can I send two arguments in a selector method?

我怕爱的太早我们不能终老 提交于 2020-01-10 04:43:47

问题


In the following, how can I send two arguments in togButton method? I tried, but it did not work.

[button addTarget:self action:@selector(togButton:) 
             forControlEvents:UIControlEventTouchUpInside];

回答1:


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).




回答2:


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:.




回答3:


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.




回答4:


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.




回答5:


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).




回答6:


[Class instancesRespondToSelector: @selector (setTo:over:)]

I remember seeing this Objective-C code in a book.



来源:https://stackoverflow.com/questions/1857817/how-can-i-send-two-arguments-in-a-selector-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!