使用故事板时继承 UIWindow [英] subclassing UIWindow while using storyboards
问题描述
我遇到了与此问题中所述相同的问题:
I have same issue as explained in this question:
我在哪里可以将我的应用程序使用的窗口从 UIWindow 更改为我自己的子类MyWindow"?带故事板?
我的问题是如何在我的应用程序委托中实现一个返回MyWindow"子类的window"getter方法?或者也许还有其他方法可以将我的子类分配给我的应用程序的主窗口?
My question is how do i implement a 'window' getter method in my app delegate that returns 'MyWindow' subclass? Or maybe there is other ways to assign my subclass to my app's main window?
推荐答案
UIWindow
在 Storyboard 项目中可以子类化,如 Apple 的 UIApplicationDelegate
参考中所述:
UIWindow
in a Storyboard project can be subclassed as explained in Apple's UIApplicationDelegate
reference:
窗口
当使用故事板时,应用程序必须呈现故事板通过将其添加到窗口并将该窗口放在屏幕上.应用程序为窗口查询此属性.保留的通过此属性引用窗口是必要的,以保持窗口被释放.如果属性的值为 nil
(默认),应用程序创建一个 UIWindow
的通用实例,并且将其分配给此属性以供委托引用.您可以实现这个协议的getter方法来提供具有不同窗口的应用程序.
window
When a storyboard is being used, the application must present the storyboard by adding it to a window and putting that window on-screen. The application queries this property for the window. The retained reference to the window by this property is necessary to keep the window from being released. If the value of the property isnil
(the default), the application creates a generic instance ofUIWindow
and assign it to this property for the delegate to reference. You may implement the getter method of this protocol to provide the application with a different window.
换句话说,在您的 AppDelegate
实现中,只需添加以下 getter
In other words in your AppDelegate
implementation simply add the following getter
目标 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 { }
}
这篇关于使用故事板时继承 UIWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!