开发者

question about selectors

I have the following:

  UITapGestureRecognizer *tapGesture;
    SEL sel = @selector(profilePop:withUser:) withObject:tapGesture withObject:message.creator;
    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:sel];

and I want it to be able to call:

- (void)profilePop:(UITapGestureRecognizer *)recognizer withUser:(Login *)user
{
    //some code here
}

b开发者_运维技巧ut why isn't it doing so?


A selector only knows about its name and any parameter names, not its arguments.

UIGestureRecognizer only accepts actions which take in either zero or one argument of its type.

In order to do what you want, you'll need to write a wrapper method to pass off a Login instance to profilePop:withUser:.

So, you'd write something like this:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(profilePopWrapper:)];
[[self view] addGestureRecognizer:tapGesture];
[tapGesture release];


//....

- (void) profilePopWrapper:(UIGestureRecognizer *) gr {
    Login *someUser = ...;
    [self profilePop:gr withUser:someUser];
}


As explained in the documentation, action messages must take a specific form

- (void)handleGesture
- (void)handleGesture:(UIGestureRecognizer *)sender

You can't just use any arbitrary selector as an action; it must be a selector that conforms to one of the patterns above.


Use single argument in @selector function this way.

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(profilePop:)];

Because UITapGestureRecognizer can identifies only tap recognition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜