【问题标题】:subclassing UIWindow while using storyboards使用情节提要时继承 UIWindow
【发布时间】:2012-05-11 07:13:27
【问题描述】:

我遇到了与此问题中解释的相同的问题:

Where can I change the window my app uses from UIWindow to my own subclass "MyWindow" with storyboard?

我的问题是如何在我的应用程序委托中实现一个返回“MyWindow”子类的“window”getter 方法?或者也许还有其他方法可以将我的子类分配给我的应用程序的主窗口?

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch uiwindow


    【解决方案1】:

    在我自己的应用程序中,我看到在从 Xcode 模板创建新应用程序时在 AppDelegate.h 中声明了“window”属性。

    此时您可以修改该属性以从“UIWindow”更改为“MyWindow”。

    或者,一个不太优雅的解决方案,您可以简单地将window 的返回值转换为“MyWindow”对象类型。

    【讨论】:

    • 这些都不会导致创建MyWindow——它们只是错误地将已经存在的UIWindow 转换为MyWindow
    • @NoahWitherspoon - vfxdomain 现在可能已经知道如何创建 MyWindow(因为他在上面的帖子中提到了 this related question)。我对他的问题的看法是,他想知道如何让“window”属性的 getter 方法返回 MyWindow。
    • @Michael Dautermann - 是的,这就是我要问的......在 App Delegate 中将 UIWindow 更改为 myWindow 不会像 Noah Witherspoon 指出的那样工作。
    • 这个答案中的第二句话几乎说“根据自己的喜好定制”,但你没有投票!人们不能推断事物吗?亲爱的先生,我会给你一个向上++
    【解决方案2】:

    Storyboard 项目中的UIWindow 可以按照 Apple 的 UIApplicationDelegate 参考中的说明进行子类化:

    窗口
    使用故事板时,应用程序必须呈现 故事板通过将其添加到窗口并将该窗口放在屏幕上。 应用程序查询窗口的这个属性。保留的 此属性对窗口的引用对于保持 窗口被释放。如果属性的值为nil( 默认),应用程序创建一个通用实例 UIWindow 和 将其分配给此属性以供委托参考。你可以 实现此协议的 getter 方法以提供 具有不同窗口的应用程序。

    换句话说,在您的 AppDelegate 实现中,只需添加以下 getter

    Objective-C

    - (MyCustomWindow *)window
    {    
        static MyCustomWindow *customWindow = nil;
        if (!customWindow) customWindow = [[MyCustomWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        return customWindow;
    }
    

    斯威夫特

    var customWindow: MyCustomWindow?    
    var window: UIWindow? {
        get {
            customWindow = customWindow ?? MyCustomWindow(frame: UIScreen.mainScreen().bounds)
            return customWindow
        }
        set { }
    }
    

    【讨论】:

    • 你能提供我如何在 Swift 中覆盖 getter 吗?
    • 非常感谢..它起作用了。我没有为此创建设置器,所以我遇到了错误。
    【解决方案3】:

    你要先继承 UIWindow 并不难

    class WinCustom : UIWindow{ 
    ....
    }
    

    然后在 AppDelegate 中:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        self.window = WinCustom(frame: UIScreen.main.bounds)
    
        self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
    
        return true
    }
    

    【讨论】:

      【解决方案4】:

      UIApplicationDelegate 协议有 window 可以使用的属性

      import UIKit
      
      class CustomWindow : UIWindow {
          //...
      }
      
      class AppDelegate: UIResponder, UIApplicationDelegate {
      
          var customWindow: CustomWindow?
      
          var window: UIWindow? {
              get {
                  customWindow = customWindow ?? CustomWindow(frame: UIScreen.main.bounds)
                  return customWindow
              }
              set { }
          }
      
          //...
      }
      

      这个解决方案只返回一个自定义 UIWindow

      [Set UIWindow]

      【讨论】:

      • 协议不决定是否计算属性。那是错误的。
      猜你喜欢
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 2012-07-22
      相关资源
      最近更新 更多