iphone - 使用 Storyboard时子类化 UIWindow

标签 iphone objective-c ios cocoa-touch uiwindow

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

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

我的问题是如何在我的应用程序委托(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 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.

换句话说,在您的 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/

相关文章:

ios - 需要在启用 ARC 的情况下使用 Leaks 进行分析吗?

ios - 将新用户对象的读/写访问权限限制为仅该用户?

iOS 和 Google Firebase - 如何执行通知操作

objective-c - 如何在 Objective C 中浏览文件夹?

ios - 数据未添加到 Plist

iphone - 计算iphone中两点之间的距离

ios - Xcode:@1x 的图像尺寸从未被使用过

iphone - 返回 autorelease NSString 仍然会导致内存泄漏

iphone - 将 int 类型的变量传递给parentViewController [iOS]

ios - 如何改变按钮的位置。 objective-c