ios - 我如何与 AppDelegate 的特定 View 进行交互?

标签 ios swift view viewcontroller appdelegate

我的 appdelegate 中有一个函数,它在通知触发时被调用。调用此函数时,我希望能够访问特定的 ViewController 并操作表格单元格,无论它是否是当前 View 。 我能够使用 rootViewController 方法让它工作,但因为它不再设置为 Root View ,所以它不再有效。

我目前正在使用这段代码:

let stopOption = UIAlertAction(title: "OK", style: .default) {
        (action:UIAlertAction)->Void in self.audioPlayer?.stop()
        print("Stop index is \(index)")
        Alarms.sharedInstance.setEnabled(false, AtIndex: index)
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let mainVC = sb.instantiateViewController(withIdentifier: "MainVC")
        dump(mainVC)
        let cells = (mainVC as! MainAlarmViewController).tableView.visibleCells
        dump(cells)
        for cell in cells
        {
            if cell.tag == index{
                print("Found the cell")
                let sw = cell.accessoryView as! UISwitch
                sw.setOn(false, animated: false)
            }
        }
    }

第一个“转储”语句返回正确的 ViewController,但它不显示任何 subview ,第二个转储语句返回“- 0 个元素”。

非常感谢任何帮助。

最佳答案

调用此函数时,我希望能够访问特定的 ViewController 并操作表格单元格,无论它是否是当前 View

为此你需要使用Notification

这里是如何使用 notificatoin 的例子 http://dev.iachieved.it/iachievedit/notifications-and-userinfo-with-swift-3-0/

您可以从 AppDelegate 发布通知并捕获到 ViewController

关于ios - 我如何与 AppDelegate 的特定 View 进行交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41971427/

相关文章:

ios - 意外的 UIPageViewController 行为

ios - 将小的 UIImage 放在大的 UIButton 中

ios - 所有用户都有完全相同的日期和时间

ios - 如何阻止系统警报并显示我的自定义屏幕

ruby-on-rails - rails 4 : conditional display not working inside view

ios - 向核心数据中输入plist数据

android - Flutter - 摆脱 iOS 上的阴影

swift - ARKit – 使用 CALayer 在 SCNNode 中加载 GIF 图像无法正确渲染

ios - 在翻译动画期间跟踪 UIView 位置

sql - 在 Redshift 中创建 View 时选择列名称