我需要從全局範圍更改故事板上的標籤的功能。如何從全球範圍更改出口屬性?
當我嘗試運行此代碼,我得到的錯誤是:
fatal error: unexpectedly found nil while unwrapping an Optional value
的問題是,viewController.congratulations爲空。
如何解決這個問題?提前致謝!
func myAlert()
{
// how to change 'configuration.text' from here?
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController : ViewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController;
viewController.congratulations.text = "test";
}
class ViewController: UIViewController
{
@IBOutlet weak var congratulations: UILabel!;
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myAlert();
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
筆尖加載後,插座獲取UI的引用,所以它始終爲零。 –
在這裏你再次按照我在'viewDidLoad' myAlert();' –
的理解進行再次探討爲什麼myAlert定義在你的類的範圍之外? –