Elliot Rodriguez
Elliot Rodriguez

Reputation: 618

Unrecognized selector sending self.window message in didFinishLaunchingWithOptions when creating a UIView

I'm working through the Big Nerd Ranch iOS Programming book (4th ed, which focuses on Objective C) using XCode 11 and I am having trouble sorting out how to add a UIView.

As soon as I send this message in didFinishLaunchingWithOptions

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

I get an unrecognized selector message.

I've tried removing the launch and main storyboards with no success, and enabling multiple windows. This is in a single view app project (perhaps I should just try an empty project?)

What am I missing or not understanding? I chose this book because I am trying to learn Objective-C (not Swift) and the debugger doesn't tell me much about what's wrong here.

Thanks.

Upvotes: 0

Views: 704

Answers (1)

Warren Burton
Warren Burton

Reputation: 17378

The tutorial dates back to pre UIWindowSceneDelegate days.

The Prewindowscene era (joke)

The reason your code is syntactically correct is that AppDelegate conforms to the protocol UIApplicationDelegate which declares window but without the property declaration on the class you'll get the exception

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate setWindow:]: unrecognized selector sent to instance ...'

If you look at the generated template project you'll probably see

AppDelegate.h/.m & SceneDelegate.h/.m

SceneDelegate.h is where you'll find the declaration:

@property (strong, nonatomic) UIWindow * window;

While you technically could add that line in

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions

That doesn't make any sense as the UIWindow instance is created by the storyboard system for you.

My instinct would be to skip that chunk of the tutorial and work through the other elements using the supplied UIWindow from SceneDelegate.

Alternatively if you have your heart set on the original tutorial you can opt out of scene mechanics and add the line

@property (strong, nonatomic) UIWindow * window;

to AppDelegate.h

Upvotes: 2

Related Questions