【问题标题】:UIAlertController:supportedInterfaceOrientations was invoked recursivelyUIAlertController:supportedInterfaceOrientations 被递归调用
【发布时间】:2015-10-03 02:26:30
【问题描述】:

当两个警报一一出现时,我的意思是一个警报出现,另一个警报出现并且应用程序崩溃。 我使用UIAlertController 来显示警报。应用程序仅在 iOS 9 设备中崩溃。

此时请帮助我。

【问题讨论】:

    标签: objective-c ios9 xcode7 uialertcontroller xcode7-beta3


    【解决方案1】:

    这是 iOS 9 中的一个错误,它无法为 UIAlertController 检索到 supportedInterfaceOrientations。在寻找supportedInterfaceOrientations for UIAlertController 时,它似乎陷入了无限递归循环(例如,它回溯到UIAlertControler -> UIViewController -> UINavigationController -> UITabBarController -> @987654329 @ -> ...),而对 UIAlertController:supportedInterfaceOrientations 的调用实际上并未在源代码中实现/覆盖。

    在我的解决方案中,我只是添加了以下代码:

    extension UIAlertController {     
        public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
            return UIInterfaceOrientationMask.Portrait
        }
        public override func shouldAutorotate() -> Bool {
            return false
        }
    }
    

    那么UIAlertController会直接返回支持的方向值,没有无限循环。希望对您有所帮助。

    编辑:Swift 3.0.1

    extension UIAlertController {
        open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return UIInterfaceOrientationMask.portrait
        }  
        open override var shouldAutorotate: Bool {
            return false
        }
    }
    

    【讨论】:

    • @Annie 您不应该使用类别来覆盖方法,未定义将调用哪个方法,Apple 强烈建议不要这样做。
    • 在 Xcode 7 和 Swift 2.1 中,我们似乎需要 supportInterfaceOrientations 来返回 UIInterfaceOrientationMask 而不是 Int。那么你也不必强制转换返回值。
    • 看起来这不再是较新的 SDK 的问题(从 iOS 10 和 11 开始)。事实上,一旦你将部署目标从 iOS 8 提升到 iOS 9,这里的解决方案会导致应用实际处于横向模式时键盘从侧面拉起等问题。
    • @CodePlumber 我刚开始在 Xcode 9.4.1、iOS 11 上看到这个问题,所以不要认为它已经修复。部署目标是 iOS 10。
    • @OscarGomez 在我的应用程序中覆盖扩展中的方法时遇到了类似的问题。我正在努力寻找说我们不应该这样做的苹果文档。你能指出我正确的方向吗?
    【解决方案2】:

    我的解决方案是 UIAlertViewController 的 Objective-C 类别。只需在任何使用 UIAlertController 的类中包含 UIAlertController+supportedInterfaceOrientations.h

    UIAlertController+supportedInterfaceOrientations.h

    //
    //  UIAlertController+supportedInterfaceOrientations.h
    
    #import <UIKit/UIKit.h>
    @interface UIAlertController (supportedInterfaceOrientations)
    @end
    

    UIAlertController+supportedInterfaceOrientations.m

    //
    //  UIAlertController+supportedInterfaceOrientations.m
    
    #import "UIAlertController+supportedInterfaceOrientations.h"
    
    @implementation UIAlertController (supportedInterfaceOrientations)
    
    #if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    #else
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    #endif
    
    @end
    

    【讨论】:

      【解决方案3】:

      作为对上面 Roland Keesom 答案的更新,这对我有用。主要区别在于supportedInterfaceOrientations 函数实际上返回的是UIInterfaceOrientationMask 而不是Int。

      在这个变体中,所有方向都支持。

      extension UIAlertController {
      
          public override func shouldAutorotate() -> Bool {
              return true
          }
      
          public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
              return UIInterfaceOrientationMask.All
          }
      }
      

      【讨论】:

        【解决方案4】:

        写一个扩展对我来说似乎是合乎逻辑的,但我明白了

        覆盖“shouldAutorotate”必须与其覆盖的声明一样可用

        执行时出错。但我找到了另一个解决方案。我编写了一个扩展 UIAlertController 的类,并覆盖了该类中的 supportedInterfaceOrientationsshouldAutorotate 函数。希望这会有所帮助。

        class MyUIAlertController : UIAlertController {
        
               override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
                      return [UIInterfaceOrientationMask.Portrait,UIInterfaceOrientationMask.PortraitUpsideDown]
               }
        
               override func shouldAutorotate() -> Bool {
                    return false
               }
        }
        

        【讨论】:

          【解决方案5】:

          我在iOS 9 beta 版本中遇到了这个问题。但似乎苹果已经在iOS 9 的最终版本中解决了。

          【讨论】:

          • 在 9.0.1 中没有修复... :-(
          • 你试过iOS 9.0吗?哪个苹果在 9 月 9 日发布了
          • 是的!我在 iOS 9.0.1 中发现了这个错误……See screenshot
          • 在 9.3 中仍然出现
          • 10年后仍然发生
          【解决方案6】:

          这也可以通过始终在新创建的 UIWindow 中显示警报控制器来解决。请参阅this SO answer,了解如何创建允许您始终以这种方式显示警报的类别。

          【讨论】:

            【解决方案7】:

            在我的项目中,我有各种支持各种方向的屏幕,因此一个通用扩展是不够的。

            我的解决方案(Swift 5.2):

            import UIKit
            
            class UIAlertControllerWithOrientationSupport : UIAlertController {
            
                var fixSupportedInterfaceOrientations: UIInterfaceOrientationMask = .allButUpsideDown
                var fixShouldAutorotate: Bool = true
            
                override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
                    return fixSupportedInterfaceOrientations
                }
            
                override var shouldAutorotate: Bool {
                    return fixShouldAutorotate
                }
            
            }
            

            【讨论】:

              猜你喜欢
              • 2014-12-09
              • 2021-01-13
              • 2016-03-21
              • 2019-10-18
              • 2011-09-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-05-02
              相关资源
              最近更新 更多