Ios 解除UIViewController后无法更新UILabel文本
我正在演示一个UIViewController而不是另一个。当我关闭显示的UIViewController并返回到当前的UIViewController并尝试更新UILabel文本时,它不会被更新。我不知道为什么。下面,Ios 解除UIViewController后无法更新UILabel文本,ios,swift,uiviewcontroller,Ios,Swift,Uiviewcontroller,我正在演示一个UIViewController而不是另一个。当我关闭显示的UIViewController并返回到当前的UIViewController并尝试更新UILabel文本时,它不会被更新。我不知道为什么。下面,customTimeViewController是我的UIViewController。单击按钮时,代理将调用my Lower函数。我试图更新我的UILabel(targetTimeText)文本,但它没有更新 我尝试了layoutifneed(),setNeedsLayout(
customTimeViewController
是我的UIViewController。单击按钮时,代理将调用my Lower函数。我试图更新我的UILabel(targetTimeText
)文本,但它没有更新
我尝试了layoutifneed()
,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()
我建议您在向上一页发送值时使用协议。问题不清楚,这会导致误解错误的概念!下次应该更加小心:)谢谢你们的回答,但我已经在使用委托在ViewControllers之间传输数据了哦!我明白了,那么通知协议呢?你试过了吗?但是,也许你的代码在某个地方是无效的,因为委托协议总是适用于我!如果你仍然不能解决这个问题,你可以发布你目前正在做的代码