ViewControllerのサンプル
main_view_controller.rb
class MainViewController < UIViewController
def viewDidLoad
super
@label = UILabel.new
@label.frame = [[10, 10], [320, 20]]
@label.font = UIFont.boldSystemFontOfSize(16)
@label.text = "Hello World"
view.backgroundColor = UIColor.whiteColor
view.addSubview(@label)
# for navigation bar
navigationItem.title = "Navigation Title"
# for tab bar
self.tabBarItem = UITabBarItem.alloc.initWithTitle("Tab1", image: nil, tag: 1)
end
end
Single View
app_delegate.rb
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = MainViewController.new
@window.makeKeyAndVisible
true
end
end
Navigation Bar View
app_delegate.rb
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
nav_controller = UINavigationController.alloc.initWithRootViewController(MainViewController.new)
@window.rootViewController = nav_controller
@window.makeKeyAndVisible
true
end
end
Tab Bar View
app_delegate.rb
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
tab1 = MainViewController.new
# tab2 = OtherViewController.new
tab_controller = UITabBarController.new
tab_controller.viewControllers = [tab1]
@window.rootViewController = tab_controller
@window.makeKeyAndVisible
true
end
end
補足
newはalloc.initと同じ意味
MainViewController.new
= MainViewController.alloc.init
tap好きな人用
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds).tap do |window|
window.rootViewController = MainViewController.new
window.makeKeyAndVisible
end