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

问题描述:

我正在处理一个ios swift应用程序,我正在介绍处理我的应用程序中的抖动手势。除了一个面板之外,我想处理应用程序周围的晃动。在这个特定的面板上,我想调用一个完全不同的功能。所以,首先我写了一个扩展的UIViewController,这样摇晃手势支持其他任何地方:为什么我不能在另一个UIViewController中重写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") 
    } 
} 

现在的问题如下所示:

当我在屏幕上摇动手机时,我想表现出不同的行为 - 我看到警报消息012g。但是当这个屏幕出现在屏幕上(它会持续几秒钟),然后我再次摇动手机 - 突然显示弹出框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并处理这个功能。