簡體   English   中英

識別UITableViewCell中的UIButton不起作用-Swift

[英]Identifying UIButton within UITableViewCell not working - Swift

我試圖通過在單元格中按下的按鈕來標識單元格索引。 完成此操作后,我試圖將信息傳遞給另一個viewController

原始文章可以在這里找到: 檢測在表視圖中按下的uibutton:Swift最佳實踐

我的代碼當前如下所示:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! questionCell

    //indicates which button was pressed
    myCell.button.tag = indexPath.row
    myCell.button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)

    return myCell
}

var selectedRow = Int()

//sets selected row variable to the row that was selected
func buttonClicked(sender:UIButton) {

    selectedRow = sender.tag
}

//sets the id of the question that was selected in the answers view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

var destinationView: AFeedTableViewController = segue.destinationViewController as! AFeedTableViewController

    destinationView.currentQuestionId = idResults[selectedRow]
}

我的問題是prepareForSegue函數在buttonClicked函數之前被調用,因此沒有更新我傳遞給另一個視圖控制器的值。 誰能解釋如何解決這個問題? 謝謝

編輯:單擊按鈕時,將執行segue

假設您的segue已連接到按鈕,則不需要額外的目標。 您可以通過prepareForSegue:sender:的發件人對象訪問按鈕prepareForSegue:sender:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    guard let button = sender as? UIButton else { return }
    guard let destinationViewController = segue.destinationViewController as? AFeedTableViewController else { return }

    destinationViewController.currentQuestionId = idResults[button.tag]
}

暫無
暫無

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

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