【问题标题】:Why I can't override a method from an extension of UIViewController in another UIViewController?为什么我不能覆盖另一个 UIViewController 中 UIViewController 扩展的方法?
【发布时间】:2016-04-20 23:45:55
【问题描述】:

我正在开发一个 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")
    }
}

现在问题如下:

当我在屏幕上摇晃手机时,我想表现得不同 - 我看到带有消息 this is some random msg 的警报消息。但是当这个屏幕出现在屏幕上时(它会持续几秒钟),我再次摇晃手机 - 突然它显示弹出窗口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)
    })

}

在从主控制器显示弹出窗口时,我可以做些什么来避免显示扩展中的弹出窗口?

【问题讨论】:

    标签: ios uiviewcontroller uialertview uialertcontroller


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多