簡體   English   中英

響應UIScrollView中以編程方式生成的自定義UIButton的觸摸

[英]Respond to touches on programmatically generated custom UIButtons inside UIScrollView

我有一個UIScrollView,其中充滿了以編程方式生成的自定義UIButton。 該代碼在整個循環中執行每次迭代,通常執行7次。

[cardButton
      addTarget:self
      action:@selector(buttonPressed:)
      forControlEvents:UIControlEventTouchUpInside];
[cardButton setTag:i + 100];
[self.scrollView addSubview:cardButton];

在其他地方我有此功能:

- (IBAction) buttonPressed:(id)sender
{
    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", [button tag]);
}

我如何鏈接兩者? 我的按鈕實際上將我需要的所有信息存儲在它的標簽內,因此我真的只需要檢測何時被點擊即可進行響應。

您已經將此行鏈接了兩個

[cardButton
      addTarget:self
      action:@selector(buttonPressed:)
      forControlEvents:UIControlEventTouchUpInside];

發件人返回您按下的按鈕的實例。

- (IBAction) buttonPressed:(id)sender
{
    UIButton *button = (UIButton *)sender;
    NSLog(@"%d", [button tag]);
    switch (button.tag) {
        case 1:
            //Action for button with tag 1
            break;
        case 2:
            //Action for button with tag 2
            break;

        default:
            break;
    }


}

暫無
暫無

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

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