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

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.

主站蜘蛛池模板: 河北省| 高雄市| 开鲁县| 漳浦县| 壤塘县| 始兴县| 思茅市| 苍南县| 肃北| 东方市| 田东县| 明星| 蓝山县| 上栗县| 门头沟区| 泰宁县| 德安县| 庆安县| 明水县| 昌图县| 略阳县| 澎湖县| 卢湾区| 应城市| 铁力市| 玉门市| 宝兴县| 新昌县| 什邡市| 光泽县| 工布江达县| 海口市| 长宁县| 固镇县| 和政县| 滕州市| 玉环县| 甘德县| 年辖:市辖区| 监利县| 高州市|