簡體   English   中英

以編程方式解開segue

[英]Programmatically unwind segue

我正在顯示一個帶有UITableView控件的UIView。 我通過調用performSegueWithIdentifier以Show segue顯示此視圖。 在調用segue的模塊中,我添加了一個我想在展開時使用的函數,以便在從segue返回時從視圖中檢索一些數據。 現在,我有一個Done按鈕,它連接到調用者的函數,這樣我就可以在它展開時從segue中檢索數據。 當點擊完成按鈕時,我不想這樣做,我想在UITableView中選擇一個項目時這樣做。

在tableView(,willSelectRowAtIndexPath)中,我嘗試調用popViewControllerAnimated。 第二個UIView關閉並將我帶回調用的UIView,但不調用unwind函數。

我知道如何使用Exit在完成按鈕中設置展開,但是當我點擊UITableView中的一行時,我無法弄清楚如何做到這一點。

這是我用來執行segue的代碼:

    @IBAction func fromButtonTap(sender: AnyObject) {
    currentButton = sender.tag

    self.performSegueWithIdentifier("UnitsSegue", sender: self)
}

這是解開segue時執行的代碼:

@IBAction func returnFromUnitSelection(segue: UIStoryboardSegue) {
    var buttonToSet: UIButton!
    var unitsToSet: UnitData!

    fromLabel.text = "0"
    toLabel.text = "0"

    if let unitsSelectionViewController = segue.sourceViewController as? UnitSelectionController {
        if currentButton == 0 {
            fromUnitData = unitsSelectionViewController.selectedUnit
            buttonToSet = fromButton
        }
        else {
            toUnitData = unitsSelectionViewController.selectedUnit
            buttonToSet = toButton
        }

        setButtonText(buttonToSet, firstLine: unitsSelectionViewController.selectedUnit.unitCode, secondLine: unitsSelectionViewController.selectedUnit.unitName, firstLineFontSize: 15, secondLineFontSize: 11)
    }
}

這是我用來關閉第二個UIView的代碼:

    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
    selectedUnit = temperatures[indexPath.row]
    self.navigationController?.popViewControllerAnimated(true)

    return indexPath
}

當我點擊完成時,展開代碼會執行。 但是當我在桌子上選擇一個項目時沒有任何反應。

您可以為整個視圖控制器創建展開segue。 為此,將segue從視圖控制器objet拖到退出對象。 在此輸入圖像描述

創建segue后,它將在文檔大綱中列出。

在此輸入圖像描述

選擇它並在屬性檢查器中為其指定標識符。 然后在你的代碼中,當tableview選擇一個單元格時,像任何其他正常的segue一樣執行此segue。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let title = tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text

    performSegueWithIdentifier("backToViewController1", sender: title)
}

然后覆蓋prepareForSegue方法將數據傳遞給第一個視圖控制器。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier where identifier == "backToViewController1",
       let destination = segue.destinationViewController as? ViewController,
           selectedTitle = sender as? String {
        destination.selectedCountryTitle = selectedTitle
    }
}

暫無
暫無

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

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