我遇到了与这个问题中解释的相同的问题:
我的问题是如何在我的应用程序委托(delegate)中实现返回“MyWindow”子类的“窗口”getter 方法?或者也许有其他方法可以将我的子类分配到我的应用程序的主窗口?
最佳答案
Storyboard项目中的
UIWindow
可以按照 Apple 的 UIApplicationDelegate
引用中的说明进行子类化:
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
Objective-C
- (MyCustomWindow *)window
{
static MyCustomWindow *customWindow = nil;
if (!customWindow) customWindow = [[MyCustomWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
return customWindow;
}
swift
var customWindow: MyCustomWindow?
var window: UIWindow? {
get {
customWindow = customWindow ?? MyCustomWindow(frame: UIScreen.mainScreen().bounds)
return customWindow
}
set { }
}
关于iphone - 使用 Storyboard时子类化 UIWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10403137/