官术网_书友最值得收藏!

Some more goodies

We know that it's not so much fun to have only a simple HelloWorld pop-up as our very first application, so let's jazz up our code by adding some more goodies to our alert box; and this time, let's do things in a much better way.

Earlier we had added an alert box in the delegate itself. Actually it is not a good idea to write code in the application delegate. It is better to write code in a Model-View-Controller (MVC) way. Right now we won't cover all three parts of the MVC architecture for now let's begin with the controller for our application and add three buttons in this alert box, add a title, and add a message for the title box.

The class UIAlertView that we've used in the last section has numerous properties, such as title, message, delegate, cancelButtonTitle, otherButtonTitles, and many more. Let's use a few of them in our application as follows:

  1. Create a file root_controller.rb in the app folder and add the following code:
    class RootController < UIViewController
        def viewDidLoad
        alert = UIAlertView.alloc.initWithTitle "This is foo title",
          message:"Do you like this example?",
            delegate: nil,
            cancelButtonTitle: "cancel"
            otherButtonTitles: "Yes","No",nil
        alert.show
        end
    end
  2. To call this controller, we need to update our AppDelegate class. Replace the following code in your app_delegate.rb file:
    class AppDelegate
      def application
        (application,didFinishLaunchingWithOptions:
          launchOptions)
        @window = UIWindow.alloc.initWithFrame
          (UIScreen.mainScreen.bounds)
        @window.rootViewController = RootController.alloc.init
        @window.rootViewController.wantsFullScreenLayout = true
        @window.makeKeyAndVisible
        true
      end
    end
  3. Start the simulator by running the rake command from the console inside your application directory as follows:

    That's cool; our earlier HelloWorld pop-up has now been replaced with an alert box that has a title, a cancel button, and two other buttons.

    Note

    The iOS SDK has been built around the MVC pattern that separates responsibilities and ends up with an application that is easy to design and maintain.

Let's understand the code

When an iPhone application starts, it puts a window on the screen, which we have created using the UIWindow class. You can think of a window as a drawing board where you can put anything, such as a button, textbox or label. The instance of the UIWindow class manages and coordinates the views of an application, which are displayed on a device screen.

A UIScreen object contains the bounding rectangle of the device's entire screen. So, UIScreen.mainScreen.bounds returns the rectangle size according to the screen size and orientation of the device.

Tip

Every iOS application needs at least one window, which is an instance of the UIWindow class.

You might be wondering, should I remember all the properties and methods of the Apple iOS SDK, such as UIAlertView? It is not necessary to memorize them as one can always refer to the properties and methods from the iOS development library. Nevertheless, having a basic idea about the usage of a class can come in handy at times. The popular IDE, RubyMine, supports RubyMotion. It also has a useful autocompletion feature.

Tip

The more you understand, the less you have to memorize.

主站蜘蛛池模板: 公主岭市| 台山市| 仪征市| 茌平县| 合水县| 浦江县| 紫阳县| 文山县| 通化县| 锡林浩特市| 洪雅县| 麻栗坡县| 鸡东县| 津南区| 巴林右旗| 酒泉市| 高安市| 高密市| 镶黄旗| 同仁县| 丽水市| 永靖县| 敖汉旗| 濮阳市| 凯里市| 同心县| 沈阳市| 百色市| 江安县| 随州市| 屯门区| 松江区| 安福县| 鄄城县| 康平县| 息烽县| 米林县| 南昌县| 酉阳| 卫辉市| 个旧市|