Vulkan
Vulkan

Reputation: 1074

How can I implement my own getter method for a custom UIWindow?

I'm trying to implement my own custom UIWindow class. It's named SNWindow. I read that you have to implement your own getter method, and that's what I did but it never get's past "Point 1". It's infinitely logging "Point 1", showing a black screen on the iPhone.

AppDelegate.h

#import <UIKit/UIKit.h>
#import "SNWindow.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) SNWindow *window;
- (SNWindow *)window;
@end

AppDelegate.m

...

- (SNWindow *)window
{
    NSLog(@"Point 1");

    //
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    //
    UIViewController *viewController = [storyboard instantiateInitialViewController];

    //
    _window = [[SNWindow alloc] init];
    _window.rootViewController = viewController;

    NSLog(@"Point 2");

    return _window;
}

Any ideas on how to fix it?

Upvotes: 0

Views: 109

Answers (1)

matt
matt

Reputation: 535315

Do not type the window property as SNWindow; type it as UIWindow, the normal way. Your code, in your App Delegate class, then needs to look like this:

- (UIWindow*) window {
    UIWindow* w = self->_window;
    if (!w) {
        w = [[SNWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self->_window = w;
    }
    return w;
}

The rest of the App Delegate, such as application:didFinishLaunching..., should just go ahead and do what it would normally do (which might be no more than return YES).

Upvotes: 2

Related Questions