从Swift中的UIWindow更改UIViewController中的IBOutlet

时间:2015-10-21 19:30:09

标签: ios swift uiviewcontroller

我有UIViewControllerIBOutlets。我还有一个UIWindow需要访问第一个IBOutlets中的UIViewControllerUIWindow。但是,每当我尝试从nil访问它时,变量都是class ViewController: UIViewController { @IBOutlet weak var playPauseLabel: UILabel! @IBOutlet weak var playButton: UIButton! @IBOutlet weak var pauseButton: UIButton! func functionA() { println(playPauseLabel) println(playButton) println(pauseButton) } } class WindowClass: UIWindow { func resetPlayPause() { var vc = ViewController() vc.functionA() } } 。这是我的一些代码。

func resetPlayPause()

现在,当我从WindowClass拨打IBOutlets时,所有三个IBOutlets都是零。我已经在这个问题上阅读了其他SO线程,但没有找到任何解决方案。如何从ViewController?{/ p>访问和修改WindowClass中的{{1}} {/ 1}}

2 个答案:

答案 0 :(得分:0)

简单的答案是resetPlayPause()每次调用ViewController时都会构造一个新实例,除非你的ViewController有一个初始化程序从故事板或xib加载自己,否则你的网点将被连接。如果您的ViewController 具有执行此操作的初始值设定项,则在加载view之前,它不会连接其出口。您只需在切换按钮之前调用vc.view中的resetPlayPause()即可完成此操作。

更难的答案是让你的窗口直接访问视图控制器的ivars似乎是一个非常糟糕的主意。将resetPlayPause()方法移动到某个地方其他而不是移动到窗口子类中,或者让它调用ViewController上的方法,该方法将在内部切换按钮。

答案 1 :(得分:0)

答案是使用NSNotificationCenter。像魅力一样。