在使用storyboards时子类化UIWindow [英] subclassing UIWindow while using storyboards

查看:161
本文介绍了在使用storyboards时子类化UIWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我的问题是如何在我的应用程序委托中实现一个'窗口'getter方法,返回'MyWindow'子类?

解决方案

UIWindow 可以按照Apple的 UIApplicationDelegate 参考中的说明对故事板项目中的code>进行子类化:


窗口

当使用故事板时,应用程序必须通过将故事板添加到窗口并将该窗口显示在屏幕上来呈现
故事板。
应用程序查询此属性的窗口。保留
引用该属性的窗口是必要的,以防止
窗口被释放。如果属性的值是 nil
默认值),应用程序创建一个 UIWindow
将其分配给此属性以供代理引用。您可以
实现此协议的getter方法,为
应用程序提供不同的窗口。




Objective-C / p>

   - (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 {}
}


I have same issue as explained in this question:

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

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 in a Storyboard project can be subclassed as explained in Apple's UIApplicationDelegate reference:

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 is nil (the default), the application creates a generic instance of UIWindow 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.

In other words in your AppDelegate implementation simply add the following 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 { }
}

这篇关于在使用storyboards时子类化UIWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆