为什么我不能在另一个 UIViewController 中覆盖 UIViewController 扩展的方法?

Why I can't override a method from an extension of UIViewController in another UIViewController?

我正在开发一个 ios swift 应用程序,我正在我的应用程序中介绍摇动手势的处理。除了一个面板之外,我想处理应用程序周围的所有晃动。在这个特定的面板上,我想调用一个完全不同的函数。所以首先我写了一个 UIViewController 的扩展,以便其他地方都支持摇动手势:

extension UIViewController:MotionDelegate {

override public func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if motion == .MotionShake {
            print("I'm in extension")
            self.showAlertMessage("", message: "I'm in extension") //this invokes an alert message 
        }
}
}

然后在我想以不同方式处理它的面板中我写道:

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
    if motion == .MotionShake {

        showAlertMsg("Yes!", message: "This is some random msg")
    }
}

现问题如下:

当我摇动屏幕上的 phone 时,我想表现得有所不同 - 我看到带有消息 this is some random msg 的警报消息。但是当这个屏幕出现在屏幕上时(它会持续几秒钟)并且我再次摇动 phone - 突然它显示弹出窗口 I'm in extension。我想避免显示第二个弹出窗口。问题是当前面有一个带有第一条消息的弹出窗口时,它无法识别我覆盖的方法。在其他情况下(当没有弹出窗口时)一切正常。这是我的 showAlertMsg 函数:

func showAlertMsg(title: String, message: String, delay: Double){


    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    self.presentViewController(alertController, animated: true, completion: nil)
    let delay = delay * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue(), {
        alertController.dismissViewControllerAnimated(true, completion: nil)
    })

}

有什么办法可以避免在显示主控制器的弹出窗口时显示扩展程序的弹出窗口吗?

因为您在面板中覆盖了 UIViewController 的扩展名。 然后你呈现 UIAlertController,它没有覆盖方法。 你可以做的是为 UIAlertController 做一个扩展并处理这个函数。