下面是每个请求的简单示例。
在我的应用程序中,我使用音频播放器来传输远程音频文件。和一个UITabBarController
举行并提出我的看法。我在我的SongsViewController
中有歌曲的桌面视图。当一行被点击时,有两件事发生...... 1)用户界面延伸到一个新的视图控制器,我的NowPlayingViewController
和2)拨打我的ControllerClass
,它处理从指定的URL获取歌曲,并开始播放。我需要我的ControllerClass
的代表为NowPlayingViewController
,这样当歌曲完成加载时,我可以发送更新到NowPlayingViewController
上的UI。所以,里面的SongsViewController
我这样做的didSelectRowAtIndexPathMethod
...
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//get a reference to the NowPlayingViewController, and set it as the delegate to the ControllerClass
let nowPlayingVC = NowPlayingViewController()
ControllerClass.delegate = nowPlayingVC
self.tabBarController.selectedIndex = 1
}
BUT!当ControllerClass
执行其代表回拨到NowPlayingVC
时,UI上没有任何事情发生!在我的头撞在墙上后...我发现原因不起作用是因为: 当一首歌被点击时,标签栏控制器正在呈现一个NowPlayingViewController
的实例,它已经有一个引用。当一首歌被点击时,我还会抓住另一个参考NowPlayingViewController
,然后我使用该实例(参考)设置为我的ControllerClass
的代表。这是一个问题,因为我的标签栏控制器所保存的NowPlayingViewController
的实例是NowPlayingViewController
的不同实例,而不是我引用并设置为我的ControllerClass
的代理的实例。这意味着当ControllerClass
去回调到NowPlayingViewController
时,它会回调到与标签栏控制器提供的实例不同的实例!因此,我没有看到像他们应该发生的任何UI变化。
在我的情况下,因为我使用的是UITabBarController
我能够做到这一点......
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//find the instance of the NowPlayingViewController that is being presented, the one that i am VIEWING and set that instacne as the delegate to the ControllerCLass
let nowPlayingVC = self.tabBarController?.viewControllers![1] as! NowPlayingVC
ControllerClass.delegate = nowPlayingVC
}
通过访问被保持的标签栏内,该视图控制器,我能够抓住到NowPlayingViewController
这是正被呈现的一个,并且因此ControllerClass
,在调用的相同的实例的引用回到它的代表,正确地回拨到由标签栏控制器呈现的NowPlayingViewController
的实例。
我假设你没有使用UITabBarController,但你的解决方案仍然是引用你的VC的SAME实例,并确保在这两个地方使用这个实例!只要确保引用SAME实例。
您需要显示一些代码;看起来你没有保持对视图控制器实例的正确引用。 – Paulw11
@ Paulw11呃哦,我应该保留一些第一个参考? – simplexity
视图控制器与数组或字典或任何其他对象没有区别。你可以有多个实例,你需要在适合iOS的短时间内参考合适的实例 – Paulw11