如何与AppDelegate中的特定视图进行交互?

时间:2017-02-01 03:30:04

标签: ios swift view viewcontroller appdelegate

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

我目前正在使用此代码:

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,但它不显示任何子视图,第二个转储语句返回" - 0个元素"。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

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

为此您需要使用Notification

以下是如何使用notificatoin issue

的示例

您可以发布AppDelegate的通知并抓住ViewController