关于选择者的问题 [英] question about selectors

查看:152
本文介绍了关于选择者的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

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

我想让它能够致电:

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

但为什么不这样做?

推荐答案

选择器只知道它的名称和任何参数名称,而不是它的参数。

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

UIGestureRecognizer 只接受接受其类型为零或一个参数的操作。

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

为了做你想做的事,你需要写一个包装器方法来将 Login 实例传递给 profilePop:withUser:

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];
}

这篇关于关于选择者的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆