移动到另一个 swift 文件时 UIAlertController 不显示

UIAlertController not showing when moved to another swift file

所以我在 swift 和 Xcode 中构建了我的单视图应用程序。我所有的代码都在一个文件中,主要 ViewController.swift 随着代码变大使事情变得更容易我已经开始将方法移动到一个单独的 swift 文件中以保持组织 - ClipManager.swift .

我有 3 种方法,它们都使用 notifyUser 调用 UIAlertController 的方法。

所以我已将此方法移动到同一个文件中,ClipManager.swift 但现在我的警报没有显示在当前 ViewController - ViewController.swift 中调用这些方法时的方法在单独的文件中。

首先是我使用 UIAlert 的方法

////Located in ClipManager.swift
class ClipManager: UIViewController {
func notifyUser(title: String, message: String) -> Void
    {
        let alert = UIAlertController(title: title,
            message: message,
            preferredStyle: UIAlertControllerStyle.Alert)

        let cancelAction = UIAlertAction(title: "OK",
            style: .Cancel, handler: nil)

        alert.addAction(cancelAction)
        self.presentViewController(alert, animated: true,
            completion: nil)
    }}

ViewController.swift

调用我的方法
class ViewController: UIViewController {
///// In ViewController.swift (where I want the Alert to show)
let SavedRecord = MyClipManager.SaveMethod(publicDatabase!, myRecord: record)

}

SaveMethod 的代码位于 ClipManager.swift

 func SaveMethod(publicDatabase: CKDatabase, myRecord:CKRecord ) -> CKRecord {

        publicDatabase.saveRecord(myRecord, completionHandler:
            ({returnRecord, error in
                if let err = error {

                    //// **** Notify called here *****
                    self.notifyUser("Save Error", message:
                        err.localizedDescription)
                } else {
                    dispatch_async(dispatch_get_main_queue()) {
                        self.notifyUser("Success",
                            message: "Record saved successfully")
                    }


                }
            }))
     return myRecord
    }

我猜我的警报没有显示,因为它们实际上是在 ClipManager.swift 上触发的,而这不在视图中。

我在这里有什么选择,将 NotifyUser 移回 ViewController.swift 并在 ClipManager.swift 中创建和对象以在我位于那里的方法中调用它?

或者有没有办法将这些警报传递给显示的视图?

你好,我会如何代替你。

对于我的 ClipManager class,它将从 NSObject 扩展。不需要 UIView 控制器

class 应该是这样的

class ClipManager: NSObject {
func notifyUser(title: String, message: String) -> Void
{
    let alert = UIAlertController(title: title,
        message: message,
        preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction = UIAlertAction(title: "OK",
        style: .Cancel, handler: nil)

    alert.addAction(cancelAction)
    UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(alert, animated: true,
        completion: nil)
}
}

为了呈现 alertView,我使用应用程序的rootViewController

您的 ViewController class 没有变化。

让我知道它是否适合你:)

我使用了 BEN MESSAOUD Mahmoud 的做事方式,但是 运行 出现了一个小错误: "Attempt to present on whose view is not in the window hierarchy!"

我对它做了一些修改来解决这个问题。我将视图控制器与对 notifyUser 的调用一起传递。然后它会从我所在的任何 VC 显示。

func notifyUser(title: String, message: String, fromController: UIViewController) -> Void{
let alert = UIAlertController(title: title,
    message: message,
    preferredStyle: UIAlertControllerStyle.Alert)

let cancelAction = UIAlertAction(title: "OK",
    style: .Cancel, handler: nil)

alert.addAction(cancelAction)
fromController.presentViewController(alert, animated: true,
    completion: nil)
}

那我就叫它:

Clipmanager.notifyUser("title", message: "message", fromController: self)

从 Sulthan 那里得到了一些帮助 link

更新 Stan 和 BEN MESSAOUD 对 Swift 3 的回答:

class ClipManager: NSObject {

func notifyUser(title: String, message: String, fromController: UIViewController) -> Void
{
    let alert = UIAlertController(title: title,
                                  message: message,
                                  preferredStyle: UIAlertControllerStyle.alert)

    let cancelAction = UIAlertAction(title: "OK",
                                     style: .cancel, handler: nil)

    alert.addAction(cancelAction)
    fromController.present(alert, animated: true, completion: nil)
}}

并称它为:

let clipMGR = ClipManager()

clipMGR.notifyUser(title: "Title", message: "Message", fromController: self)