Interactive Debugging

Earlier we mentioned that RubyMotion ships with an interactive debugger. This is what appeared in our terminal after running our first app in Where It Begins: AppDelegate. Any code entered in the debugger is executed line by line, and the changes you make are reflected immediately in the simulator.

The debug console can be a really powerful tool not only for debugging but also for rapid prototyping. We can try different fonts and colors or play with the position of UI elements without recompiling the app each time. But all that versatility may make it seem daunting to actually figure out how to use the console effectively. Let’s play with our app in the console and see if we can have some fun.

Let’s rake our app one more time. After compilation and setup, the terminal should display a prompt while the simulator is open. Because of our puts in AppDelegate, the Hello from the console! should be above the highlighted prompt. Don’t close the UIAlertView yet because we’re going to play with it.

Let’s find the instance of our AppDelegate. Remember that it’s set as our application delegate, so first we probably need to grab information about our application. Luckily, the UIApplication class represents an application, and we access our application’s instance using UIApplication.sharedApplication.

 (main)> app = UIApplication.sharedApplication
 => #<UIApplication>

Care to take a guess how we get our AppDelegate out of that? Probably a delegate method, right?

 (main)> delegate = app.delegate
 => #<AppDelegate>

Excellent; now we can grab our UIAlertView using Ruby’s instance_variable_get. We can change the alert’s message and title and run the commands in the relevant code:

 (main)> alert = delegate.instance_variable_get("@alert")
 => #<UIAlertView>
 (main)> alert.title = "Goodbye"
 => "Goodbye"
 (main)> alert.message = "You say yes?"
 => "You say yes?"
 (main)> alert.show

The results show immediately:

images/first_app/debug_alert.png

Finally, we can programmatically hide our alert with alert.dismiss. Even though we used only this one alert object, we were able to interact with and change our app’s UI with a few easy commands.

See, getting up and running with RubyMotion wasn’t so terrible. With just motion create and rake, we started a new project and were able to run and debug our app. And if you ask me, that’s a sight easier than booting up Xcode and going through its many project creation wizards and configuration screens. Plus, the RubyMotion debugging workflow is miles more intuitive than using the low-level gdb and lldb tools in Xcode. In all, RubyMotion makes for a buttery-smooth transition into mobile development rather than jumping into the jarring world of Xcode and Objective-C.

But this is just the start: we need to put elements that are more interesting than blue alerts on the screen. In the next chapter, Chapter 2, Filling the Screen with Views, we’ll cover how to place and animate basic boxes, buttons, and labels in our app.

Footnotes

[3]

For detailed installation instructions, visit the RubyMotion documentation at http://www.rubymotion.com/developer-center/guides/getting-started/.

[4]

Check out the RubyMotion documentation at http://www.rubymotion.com/developer-center/articles/editors/ for a short list.

[5]

Named arguments like those in RubyMotion are available as of Ruby 2.0.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset