Last Updated: February 25, 2016
·
1.863K
· brendanjcaffrey

Your RubyMotion AppDelegate needs a @window accessor

I've seen a lot of code for AppDelegate classes in RubyMotion that looks a lot like this:

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @window.rootViewController = UIViewController.new // ...
    @window.makeKeyAndVisible
    true
  end
end

But there's a tiny error in this code. It's small, but it's still there, and it's relatively pervasive too.

What's wrong with it? Take a look at Apple's documentation for UIApplicationDelegate:

window

If you want to provide a custom window for your app, you must implement the getter method of this property and use it to create and return your custom window.

How to fix it? Simple:

class AppDelegate
  attr_reader :window

  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @window.rootViewController = UIViewController.new // ...
    @window.makeKeyAndVisible
  end
end

Is this pedantic? Absolutely! But I was scratching my head trying to use the MRProgress CocoaPod, and it depends on window being implemented.

1 Response
Add your response

This seems like it's better way to factor the creation and memoization of the window: https://gist.github.com/tpitale/7807091

over 1 year ago ·