【问题标题】:UIAlertController not showing when moved to another swift file移动到另一个 swift 文件时 UIAlertController 不显示
【发布时间】:2016-04-28 09:07:54
【问题描述】:

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

我有 3 个方法,它们都使用调用 UIAlertControllernotifyUser 方法。

因此,我已将此方法移至同一文件 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 上触发的,而ClipManager.swift 不在视图中。

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

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

【问题讨论】:

    标签: ios swift methods viewcontroller uialertcontroller


    【解决方案1】:

    你好,我将如何代替你。

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

    这里的类应该是什么样子

    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 类中无需进行任何更改。

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

    【讨论】:

    • 非常感谢!这工作得很好......你能向我解释一下这是如何改变的吗,我知道 rootviewcontroller 正在选择第一个视图控制器?为什么要改成NSObject?
    • 对于 NSObject 它只是告诉您可以使用简单的 swift 类进行通知管理。 rootView 控制器是包含您的内容的根视图
    • NSObject 部分与解决方案无关,Swift 中的所有内容都继承自 NSObject,在类文件中显式继承它与不包含它没有区别。
    • 是的,我知道。我这样做只是为了表明已经修改了 ClipManager 超类。感谢@pbush25 的岁差 :)
    【解决方案2】:

    我使用了 BEN MESSAOUD Mahmoud 的做事方式,但有时遇到了一个小错误: "尝试呈现不在窗口层次结构中的视图!"

    我最终改变了一点来解决这个问题。我将视图控制器与对 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)
    

    在此链接 AlertController is not in the window hierarchy 上从 Sulthan 获得了一些帮助

    【讨论】:

      【解决方案3】:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多