如何从 iOS AppDelegate 调用视图控制器函数? [英] How can I call a view controller function from an iOS AppDelegate?

查看:18
本文介绍了如何从 iOS AppDelegate 调用视图控制器函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到来自远程推送通知的电话,其中包含我想要显示的图像路径.

I am getting a call from a remote push notification, which includes a path to an image, which I want to display.

AppDelegate 是:

The AppDelegate is:

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
}

...视图如下所示:

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!)
        }
    }
}

我收到一个异常,说 imgView 为空(它被声明为 @IBOutlet weak var poolImage: UIImageView!,我可以通过调用 loadImage("http://lorempixel.com/400/200/") 来自按钮点击.

I'm getting an exception saying imgView is null (it is declared as @IBOutlet weak var poolImage: UIImageView! and I can display an image by calling loadImage("http://lorempixel.com/400/200/") from a button click.

我在 stackoverflow 上找到了几个相关的答案(上面引用了一个),但都不起作用.

I found a couple of related answers on stackoverflow (one is referenced above) but none work.

推荐答案

如果您想以模态方式显示新的视图控制器,请进一步查看 rach 的回答.

If you want to show a new view controller modally, then look into rach's answer further.

如果您的 ViewController 已经在屏幕上,您需要更新它以显示您需要的信息.

If you're ViewController is already on screen, you will need to update it to show the information you need.

您如何执行此操作取决于您的根视图控制器设置.您的视图控制器是否嵌入在 UINavigationController 中?如果是这样,那么试试这个:

How you do this will depend on your root view controller setup. Is your view controller embedded in an UINavigationController? If so then try this:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if let rootViewController = window?.rootViewController as? UINavigationController {
        if let viewController = rootViewController.viewControllers.first as? ViewController {
            let url = alert["imgurl"] as? NSString
            viewController.loadImage(url as String)
        }
    }
}

如果不是,那么试试这个:

If it is not, then try this:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if let rootViewController = window?.rootViewController as? ViewController {
        let url = alert["imgurl"] as? NSString
        rootViewController.loadImage(url as String)
    }
}

这篇关于如何从 iOS AppDelegate 调用视图控制器函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆