LoginSignup
21
20

More than 5 years have passed since last update.

RubyMotionのAppDelegateに書くべき基本的なViewのテンプレート

Posted at

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
21
20
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20