当我的 iOS 应用程序从后台状态变为活动状态时,我正在尝试更改标签的文本或显示警报。
当我在 ViewController 类中调用一个函数时,只有 print() 方法可以正常工作。但是当我想与该类中的对象交互时,它会显示错误。
SceneDelegate.swift:
var vc = ViewController()
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
vc.showMessage("Test message")
}
ViewController.swift:
@IBOutlet weak var textLabel: UILabel!
func showMessage(_ incomingMessage:String!) {
let warning = UIAlertController(title: "Warning", message: incomingMessage, preferredStyle: .alert)
let aButton = UIAlertAction(title: "OK", style: .cancel, handler: nil)
warning.addAction(aButton)
self.present(warning, animated: true)
textLabel.text = incomingMessage
print("message is : " + incomingMessage)
}