ios - 在重复的 UIViewsController 之间定位特定的 UIViewController 实例

标签 ios xcode swift uiviewcontroller

我有一个带有 View 计数器的 UIViewController (cardView)。右上角有一个“菜单”按钮,用于打开一个单独的 UIViewController (cardsViewer),其中列出了所有不同的卡片并作为模式打开。

在cardsViewer内部,您可以单击列表上的按钮之一,它会覆盖相同类型的UIViewController(cardView)的模式并显示相同的 View 计数器,但不是“菜单”按钮,而是有一个“返回”按钮。

问题是当我转到cardView的新实例时,如果它与原始实例相同,则 View 计数器会在cardView的第二个实例上更新,但是当您返回到原始cardView并发送时它(通过委托(delegate)函数)更新的 viewCount,它不会在刷新时更新标签。

我什至让委托(delegate)函数打印显示已收到的 View 计数,但它不会对 View 执行任何操作。即使在 View 的第二个实例被关闭但第一个实例仍未更新后,也会发生这种情况。

这几乎就像是,一旦我创建了第二个实例,我就无法再修改第一个实例的 UI。我使用了 dispatch get main queue 等等,但都没有用。

帮助!

最佳答案

这是针对每个请求的一个简单示例。

在我的应用程序中,我使用音频播放器来传输远程音频文件。还有一个 UITabBarController 来保存和呈现我的观点。我的 SongsViewController 中有一个歌曲的表格 View 。当点击一行时,会发生两件事... 1) UI 转到一个新的 View Controller ,即我的 NowPlayingViewController 和 2) 对我的 ControllerClass 进行调用,它处理从指定 URL 获取歌曲并开始播放。我需要将 ControllerClass 的委托(delegate)设为 NowPlayingViewController,以便当歌曲加载完成时,我可以向 NowPlayingViewController 上的 UI 发送更新>。因此,在 SongsViewControllerdidSelectRowAtIndexPathMethod 中,我执行了此操作...

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
}

但是!当 ControllerClass 执行对 NowPlayingVC 的委托(delegate)回调时,UI 上没有任何反应!在我把头撞在墙上之后……我发现这不起作用的原因是: 当点击一首歌曲时,标签栏 Controller 将呈现一个它已经保存了引用的 NowPlayingViewController 实例。当点击一首歌曲时,我还会获取对 NowPlayingViewController 的另一个引用,然后使用该实例(引用)将其设置为我的 ControllerClass 的委托(delegate)。这是一个问题,因为我的标签栏 Controller 所持有的 NowPlayingViewController 实例与我引用并设置为的 NowPlayingViewController 实例不同。委托(delegate)给我的 ControllerClass 。这意味着当 ControllerClass 回调到 NowPlayingViewController 时,它会回调到与选项卡栏 Controller 所呈现的实例不同的实例!因此,我没有看到用户界面发生任何变化,就像他们应该做的那样。

就我而言,由于我使用的是 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

 }

通过访问标签栏内的 View Controller ,我能够获取对正在呈现的 NowPlayingViewController 的同一实例的引用,因此 ControllerClass 在回调其委托(delegate)后,正确回调到由选项卡栏 Controller 呈现的 NowPlayingViewController 实例。

我假设您没有使用 UITabBarController,但您的解决方案仍然是引用 VC 的同一实例,并确保在两个地方都使用该一个实例!只需确保引用相同的实例即可。

关于ios - 在重复的 UIViewsController 之间定位特定的 UIViewController 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35881540/

相关文章:

ios - 使用外部配置文件配置 iOS 应用程序

iphone - Audio Session 属性监听器在 UIImagePickerController 中自动停用?

swift - 双内容 xml 快速转换为 struct 结构

ios - 如何通过按钮索引 int 数组?

ios - 从 UITapGestureRecognizer 访问 subview

iOS ZXingWidget - 在自己的 ViewController.view 中使用 ZXingWidgetViewController 的 View 作为 subview

ios - UIPageViewController 的第一页显示黑屏且未加载内容

c++ - 在 Xcode 中,当我包含 <iostream> 时,stdlib.h 似乎也包含在内。为什么?

ios - 从字符串初始化 NSItemProvider 的问题

ios - 如何在单击 Collection View 上的单元格时在详细 View 上获得相同的图像?