我在另一个上展示了一个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)
}
答案 0 :(得分:0)
在这种情况下,您可以使用通知协议或代理协议。尝试使用协议的此代码,并将变量或函数名称更改为当前代码。
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
}
}
答案 1 :(得分:-1)
我解决了,
我通过调用addSubview()方法在代码中添加了UILabel(targetTimeText),如果我调用setNeedsLayout()添加了它,那么一切正常。请在下面检查
self.view.addSubview(targetTimeText)
view.setNeedsLayout()