xcode IOS:给@selector 添加一个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8229370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:31:20  来源:igfitidea点击:

IOS: add a parameter to @selector

iosxcodeparameters

提问by cyclingIsBetter

When I have this line of code

当我有这行代码时

UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(dragGestureChanged:)];

and this

和这个

- (void)dragGestureChanged:(UILongPressGestureRecognizer*)gesture{
...
}

I want to add at "@selector(dragGestureChanged:)" a parameter that is "(UIScrollView*)scrollView", how can I do?

我想在“@selector(dragGestureChanged:)”处添加一个“(UIScrollView*)scrollView”参数,我该怎么做?

回答by Tommy

You can't directly — UIGestureRecognizers know how to issue a call to a selector that takes one argument only. To be entirely general you'd probably want to be able to pass in a block. Apple haven't built that in but it's fairly easy to add, at least if you're willing to subclass the gesture recognisers you want to get around the issue of adding a new property and cleaning up after it properly without delving deep into the runtime.

你不能直接 — UIGestureRecognizers 知道如何调用只接受一个参数的选择器。为了完全通用,您可能希望能够传入一个块。Apple 还没有内置它,但它很容易添加,至​​少如果你愿意将手势识别器子类化,你想要解决添加新属性并在它之后正确清理的问题,而无需深入研究运行时.

So, e.g. (written as I go, unchecked)

所以,例如(随我写,未选中)

typedef void (^ recogniserBlock)(UIGestureRecognizer *recogniser);

@interface UILongPressGestureRecognizerWithBlock : UILongPressGestureRecognizer

@property (nonatomic, copy) recogniserBlock block;
- (id)initWithBlock:(recogniserBlock)block;

@end

@implementation UILongPressGestureRecognizerWithBlock
@synthesize block;

- (id)initWithBlock:(recogniserBlock)aBlock
{
    self = [super initWithTarget:self action:@selector(dispatchBlock:)];

    if(self)
    {
         self.block = aBlock;
    }

    return self;
}

- (void)dispatchBlock:(UIGestureRecognizer *)recogniser
{
    block(recogniser);
}

- (void)dealloc
{
    self.block = nil;
    [super dealloc];
}

@end

And then you can just do:

然后你可以这样做:

UILongPressGestureRecognizer = [[UILongPressGestureRecognizerWithBlock alloc] 
        initWithBlock:^(UIGestureRecognizer *recogniser)
        {
            [someObject relevantSelectorWithRecogniser:recogniser 
                      scrollView:relevantScrollView];
        }];

回答by trojanfoe

So the method will look like this:

所以该方法将如下所示:

- (void)dragGestureChanged:(UILongPressGestureRecognizer*)gesture
    scrollView:(UIScrollView *)scrollview
{
    ...
}

The selector will look like this:

选择器将如下所示:

UILongPressGestureRecognizer *downwardGesture = [[UILongPressGestureRecognizer alloc]
    initWithTarget:self action:@selector(dragGestureChanged:scrollView:)];