簡體   English   中英

具有參數的UIButton選擇器方法或在創建按鈕時定義選擇器方法

[英]UIButton selector method with parameters or define the selector method on button creation

我有一個表,其中每個行對象可以具有特定的內容(文本,帶有URL的文本-結果是在Safari中打開鏈接-和圖像)。

我要做的是為每個細節創建一個視圖,但是例如為帶有URL的文本創建一個帶有選擇器方法的按鈕,而我沒有機會為該選擇器設置URL參數-它始終顯示最后創建的網址。

有什么方法可以讓代碼創建按鈕時打開鏈接? (我有正確的值。)

在選擇器方法中使用UIButton *sender參數找到與所點擊的按鈕關聯的行,然后獲取與該項關聯的URL。

您的選擇器方法應如下所示:

- (void)didTapLinkButtonInCell:(UIButton *)sender {
     CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
     NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];

     // If you have the items of your table in an array named 'items', for example
     NSLog("Clicked URL is %@". self.items[indexPath.row].url);
}

如果您需要訪問按下按鈕的單元格,也可以使用[cellForRowAtIndexPath:indexPath]

暫無
暫無

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

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