简体   繁体   English

Swift 4.2-如何在枚举函数中使用警报?

[英]Swift 4.2 - How to use an Alert in a enum function?

I got this 我懂了

#if os(iOS)
import UIKit
import Foundation
#elseif os(OSX)
import Foundation
import cocoa
#else
// Future concerns
#endif


enum Image: String {
    case Preferences
    ....
    case App
    case Stop
    ....
    case Default


    #if os(iOS)
    func image(selected: Bool = false) -> UIImage? {
        let imgName : String = selected ? self.rawValue + "-selected" : self.rawValue
        if let img = UIImage(named:imgName) {
            return img
        } else {
            print("iOS")
            print("Please add the \(imgName) icon to the app assets!!")
            //Creating the alert
            let alertController = UIAlertController(title: "Icon Missing", message: "Please add the \(imgName) image to the app assets!!", preferredStyle: .alert)
            let action = UIAlertAction(title: "I Promiss I will", style: .default, handler: nil)
        alertController.addAction(action)
        // How to get the alert working
     self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
       //
            return nil
        }
    }
    #elseif os(OSX)
    func image(selected: Bool = false) -> NSImage? {
        ....
    }
    #else
    //
    #endif
}

I know enum doesn't have a self.window, I just type this line (self.window?.rootViewController?...) to show the issue. 我知道枚举没有self.window,我只需键入以下行(self.window?.rootViewController?...)来显示问题。 Is there away to get the alert working? 有没有办法让警报正常工作? Of course I don't want to submit a view object to the Image.Question.image(). 当然,我不想将视图对象提交给Image.Question.image()。 Also if it will be used in a later phase in the development will be debatable, but still want to know if there is a way. 另外,如果将其用于开发的后续阶段将是值得商,的,但仍然想知道是否有办法。

Thank you in advance. 先感谢您。

======== Solved ========= ========解决了=========

With the info and comment from k.zoli, my main issue did get solved. 有了k.zoli的信息和评论,我的主要问题确实得到了解决。

resulting code 结果代码

func image(selected: Bool = false) -> UIImage? {
        let imgName : String = selected ? self.rawValue + "-selected" : self.rawValue
        guard let img = UIImage(named: imgName) else {
            print("iOS")
            print("Please add the \(imgName) icon to the app assets!!")
            //Creating the alert
            let alertController = UIAlertController(title: "Icon Missing", message: "Please add the \(imgName) icon to the app assets!!", preferredStyle: .alert)
            let action = UIAlertAction(title: "I Promiss I will", style: .default, handler: nil)
            alertController.addAction(action)
            if let window = UIApplication.shared.delegate?.window { DispatchQueue.main.async { window?.rootViewController?.present(alertController, animated: true, completion: nil) } }
            return nil
        }
        return img
    }

You can get access to the current window through UIApplication's shared instance. 您可以通过UIApplication的共享实例访问当前窗口。

if let window = UIApplication.shared.delegate?.window {
        window?.rootViewController?.present(alertController, animated: true, completion: nil)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM