- RubyMotion iOS Develoment Essentials
- Abhishek Nalwaya Akshat Paul
- 503字
- 2021-08-13 16:43:55
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:
- Create a file
root_controller.rb
in theapp
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
- To call this controller, we need to update our
AppDelegate
class. Replace the following code in yourapp_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
- 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.
- Docker技術入門與實戰(第3版)
- Access 數據庫應用教程
- Raspberry Pi for Secret Agents(Third Edition)
- Python應用輕松入門
- WordPress 4.0 Site Blueprints(Second Edition)
- Android驅動開發權威指南
- 深度學習原理與PyTorch實戰(第2版)
- PyQt編程快速上手
- 深度實踐KVM:核心技術、管理運維、性能優化與項目實施
- 人人都能開發RPA機器人:UiPath從入門到實戰
- Mastering PowerCLI
- 你必須知道的.NET(第2版)
- Java EE互聯網輕量級框架整合開發:SSM+Redis+Spring微服務(上下冊)
- Java Web程序開發參考手冊
- Mathematica Data Visualization