以实时(iOS)Swift 2将数据从AppDelegate传递到ViewController
我想使用apple notification创建聊天,我想在收到通知时将数据传递到我的表视图以进行更新 我正在尝试做一个示例来测试如何将数据表单应用程序委托传递给viewController,但它不起作用。有人能帮忙吗 我的ViewController代码以实时(iOS)Swift 2将数据从AppDelegate传递到ViewController,ios,iphone,swift2,Ios,Iphone,Swift2,我想使用apple notification创建聊天,我想在收到通知时将数据传递到我的表视图以进行更新 我正在尝试做一个示例来测试如何将数据表单应用程序委托传递给viewController,但它不起作用。有人能帮忙吗 我的ViewController代码 class ViewController: UIViewController { var fileDirectory: String = String() let delegate = UIApplication.sharedApp
class ViewController: UIViewController {
var fileDirectory: String = String()
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.refreshView(_:)), name: "refreshView", object: nil)
}
func refreshView(notification: NSNotification) {
fileDirectory = delegate.filePath
print("tester")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我想从appDelegate访问refreshView函数,您在下面写的行就在viewDidLoad()中的ViewController中 但在appDelegate中要调用ViewController.refreshView方法的位置添加此行
NotificationCenter.default.post(name: "AnyThingYouWant", object: self)
}
若你们想在收到通知时调用refreshView方法,那个么在didReceiveLocal/RemoteNotification方法中的第行上方写下你们在viewDidLoad()中的ViewController中写下的第行就在右边 但在appDelegate中要调用ViewController.refreshView方法的位置添加此行
NotificationCenter.default.post(name: "AnyThingYouWant", object: self)
}
如果您想在收到通知时调用refreshView方法,请在didReceiveLocal/RemoteNotification方法中的第行上方写入是否有使用通知中心的特定原因?如果这是唯一需要知道何时收到通知的视图控制器,我会直接更新视图控制器。几年前我回答了一个非常相似的问题:不工作的人我试图打印“离开”,也不工作它不去如果{}我尝试了你的两种方法,都不工作我需要在视图控制器类中制作任何东西吗?如果让rootViewController=window?.rootViewController为?ViewController{print(“Away”)rootViewController.TestNotificationPasse(“TesTef”)}使用通知中心有具体原因吗?如果这是唯一需要知道何时收到通知的视图控制器,我会直接更新视图控制器。几年前我回答了一个非常相似的问题:不工作的人我试图打印“离开”,也不工作它不去如果{}我尝试了你的两种方法,都不工作我需要在视图控制器类中制作任何东西吗?如果让rootViewController=window?.rootViewController为?ViewController{print(“Away”)rootViewController.TestNotificationPasse(“TesTef”)}