簡體   English   中英

我不明白為什么這個@selector不起作用

[英]I don't get why this @selector isn't working

我正在創建幾個UIView ,我添加了一個UITapGestureRecognizer其中一個action假定在viewController調用一個方法,但它似乎不起作用。

-(void)setupFreeMoneyView {
    NSArray *array = @[@"Facebook", @"Twitter", @"SMS", @"Email"];
    int index = 0;
    for (NSString *str in array) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100*index++, 0, 100, 100)];
        [imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@Logo", str]]];
        [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:NSSelectorFromString([NSString stringWithFormat:@"postTo%@", str])]];
        [freeView addSubview:imgView];
    }
}

freeView是在MainStoryboard創建的UIView

我通過使用普通的@selector()來檢查它的NSSelectorFromString()是否不起作用,但它不是原因。

如果我更改行[imgView addGestureRecognizer:... to [freeView addGestureRecognizer:...]它確實有效

我似乎不明白為什么會這樣。

嘗試這個

imgView.userInteractionEnabled = YES ;

默認情況下, UIImageView userInteractionEnabled設置為NO。

也許你需要為UIImageView設置userInteractionEnabled = YES

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM