Wie kann ich eine View Controller-Funktion von einem iOS AppDelegate aus aufrufen?
Ich erhalte einen Anruf von einer Remote-Push-Benachrichtigung, die einen Pfad zu einem Bild enthält, das ich anzeigen möchte.
The AppDelegate ist:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
// get url from notification payload (shortened version)
let url = alert["imgurl"] as? NSString
let vc = ViewController()
vc.loadImage(url as String) // cannot find the imageView inside
}
... und die Ansicht sieht so aus:
func loadImage(url:String) {
downloadImage(url, imgView: self.poolImage)
}
// http://stackoverflow.com/a/28942299/1011227
func getDataFromUrl(url:String, completion: ((data: NSData?) -> Void)) {
NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) { (data, response, error) in
completion(data: NSData(data: data!))
}.resume()
}
func downloadImage(url:String, imgView:UIImageView){
getDataFromUrl(url) { data in
dispatch_async(dispatch_get_main_queue()) {
imgView.image = UIImage(data: data!)
}
}
}
Ich bekomme eine Ausnahme mit der AufschriftimgView
ist null (es wird als @ deklarier@IBOutlet weak var poolImage: UIImageView!
und ich kann ein Bild anzeigen, indem ich @ anruloadImage("http://lorempixel.com/400/200/")
Klicken Sie auf eine Schaltfläche.
Ich habe ein paar verwandte Antworten zum Stackoverflow gefunden (eine wird oben genannt), aber keine funktioniert.