Showing a Message with UIAlertView

Now that we understand how our app boots up and we have our entry point, let’s try something. We’re going to show a UIAlertView, which is the standard blue pop-up you see when you get an error message or push notifications pre-iOS5. Let’s change application:didFinishLaunchingWithOptions: to add an alert right before returning.

 class​ AppDelegate
 def​ application(application, didFinishLaunchingWithOptions​:launchOptions​)
» @alert =
» UIAlertView.alloc.initWithTitle(​"Hello"​,
»message: ​​"Hello, RubyMotion"​,
»delegate: ​​nil​,
»cancelButtonTitle: ​​"OK"​,
»otherButtonTitles: ​​nil​)
» @alert.show
»
» puts ​"Hello from the console!"
 
 true
 end
 end

We create our alert using UIAlertView.alloc.initWith..., which is a prime example of the verbose Objective-C syntax used in the iOS SDK. The initializer here takes many arguments, but the only ones we care about are title, message, and cancelButtonTitle. Once those are set, all we need to do is call show, and the alert will appear.

We also added a call to the logging statement puts. You can pass it a normal string to print verbatim, or you can pass it any normal object that you want some information about. This will be the output visible in the debugging console while our app runs.

Speaking of which, let’s run rake one more time and check out our blue pop-up! (See Figure 2, A UIAlertView in your app.)

images/first_app/1.png

Figure 2. A UIAlertView in your app

In your terminal window, you should see (main)> Hello from the console! output in the emulator. While we’re looking here, let’s take a deeper look into the RubyMotion debugger.

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

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