从视图控制器中处理远程通知

时间:2018-07-24 01:07:36

标签: ios swift

我正在尝试从特定的视图控制器中处理远程通知。我已经设置好我的应用程序,以便在我推送远程通知时在ImageView中向特定的视图控制器显示特定的图像,但是如果不再次按下相同的控制器就无法更改图像。呈现视图控制器后,我正尝试从另一个远程通知更改ImageView的图像。我实现此目标的想法是,创建一种方法来处理刚刚显示的视图控制器文件中的远程通知,以便远程通知可以编辑ImageView的图像。这就是我在AppDelegate中显示视图控制器的方式:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if let data = userInfo["showiMac"] as? [String: String], let iMacConfig = data["iMacConfig"] {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootTabBarController = self.window?.rootViewController as! UITabBarController
        let firstNavigationController = storyboard.instantiateViewController(withIdentifier: "initialVC1") as! UINavigationController
        rootTabBarController.viewControllers![0] = firstNavigationController

        //the viewController to present
        let viewController = storyboard.instantiateViewController(withIdentifier: "configureiMac") as! ConfigureiMacViewController

        // present VC
        firstNavigationController.pushViewController(viewController, animated: true, completion: {
            viewController.image = UIImage(named: "iMac" + iMacConfig)
        })

        completionHandler(.noData)


    }
}

这是在我的视图控制器中声明图像的方式:

var image: UIImage! 

override func viewDidLoad() {
     super.viewDidLoad()
     imageView.image = image 
}

1 个答案:

答案 0 :(得分:0)

在您的应用程序委托中,执行以下操作:

  weak var configureiMacViewController: ConfigureiMacViewController?

创建帮助器功能以保持代码的清洁和可读性:

private func updateiMacImage(_ image: UIImage, in existingViewController: ConfigureiMacViewController) {
    existingViewController.image = image
}

private func showiMacImage(_ image: UIImage) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let rootTabBarController = self.window?.rootViewController as! UITabBarController
    let firstNavigationController = storyboard.instantiateViewController(withIdentifier: "initialVC1") as! UINavigationController
    rootTabBarController.viewControllers![0] = firstNavigationController

    //the viewController to present
    let viewController = storyboard.instantiateViewController(withIdentifier: "configureiMac") as! ConfigureiMacViewController
    configureiMacViewController = viewController

    // present VC
    firstNavigationController.pushViewController(viewController, animated: true, completion: {
        viewController.image = UIImage(named: "iMac" + iMacConfig)
    })
}  

然后更新:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if let data = userInfo["showiMac"] as? [String: String], let iMacConfig = data["iMacConfig"] {
        let image = UIImage(named: "iMac" + iMacConfig)
        if let existingViewController = configureiMacViewController {
           updateiMacImage(image, in: existingViewController)
        } else {
           showiMacImage(image)
        }    
        completionHandler(.noData)
    }
}

您的AppDelegate中的弱变量将存储对视图控制器的引用,但是如果使用“弱”关键字,则如果视图控制器被关闭,它将允许其从内存中释放,在这种情况下,下一次您将变量设为nil尝试访问它。如果视图控制器仍然可见,则将仍然定义其值,您将能够重新使用它来更新图像。

您很可能还必须将ConfigureiMacViewController代码更新为:

var image: UIImage! {
    didSet {
        imageView.image = image
    }
}

override func viewDidLoad() {
     super.viewDidLoad()
     imageView.image = image 
}

这可以确保如果已经加载并显示了视图控制器,但是您更新了image变量,则将最新的图像加载到图像视图中。

祝你好运!