簡體   English   中英

關閉 UIViewController 后無法更新 UILabel 文本

[英]Cannot update UILabel text after dismissing UIViewController

我在另一個上展示了一個 UIViewController。 當我關閉呈現的 UIViewController 並返回到當前的 UIViewController 並嘗試更新 UILabel 文本時,它沒有更新。 我不知道為什么。 下面, customTimeViewController是我展示的 UIViewController。 當一個按鈕被點擊時,我的下面的函數被委托調用。 我嘗試更新我的 UILabel ( targetTimeText ) 文本,但它沒有更新。

我嘗試了layoutIfNeeded()setNeedsLayout()setNeedsDisplay() 沒有任何幫助。

func selectedCustomTime(minutes: Int, seconds: Int) {
    customTimeViewController?.dismiss(animated: true, completion: nil)
    self.targetTimeText.text = GameTimeUtility.convertTimeToHumanReadable(time: minutes*60+seconds)
}

在這種情況下,您可以使用通知協議或委托協議。 試試這個使用協議的代碼並將變量或函數名稱更改為您當前的代碼。

protocol MydataDelegate{
    func passingData(mystring: String)
}

Class SecondViewController: UIViewController{

    var mdataDelegate: MydataDelegate!

    func selectedCustomTime(minutes: Int, seconds: Int) {
        mdataDelegate.passingData(mystring: GameTimeUtility.convertTimeToHumanReadable(time: minutes*60+seconds))
        customTimeViewController?.dismiss(animated: true, completion: nil)
    }

}

Class FirstViewController: UIViewController,MydataDelegate{

    func openSecondVC(){

        // Mark: Try your code that open new viewcontroller but don't forget pass your delegate
        let mainStoryboard = UIStoryboard(name: "Storyboard", bundle: NSBundle.mainBundle())
        let vc = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
        vc.mdataDelegate = self
        self.presentViewController(vc, animated: true, completion: nil)
    }

    func passingData(mystring: String){
        self.targetTimeText.text = mystring
    }

}

我解決了伙計們

我通過調用 addSubview() 方法在代碼中添加了我的 UILabel (targetTimeText)。如果我調用 setNeedsLayout() 添加它之后,那么一切正常。 請檢查以下

self.view.addSubview(targetTimeText)
view.setNeedsLayout()

暫無
暫無

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

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