使用RubyMotion单击按钮时如何加载控制器?

mar*_*cgg 1 ruby iphone uiviewcontroller ios rubymotion

假设我有2个控制器A和B.

在AI中有:

def viewDidLoad
  super
  button = UIButton.buttonWithType UIButtonTypeRoundedRect
  button.setTitle "Open B", forState: UIControlStateNormal
  button.addTarget(self, action: :open_b, forControlEvents: UIControlEventTouchUpInside)
  self.view.addSubview button
end

def open_b
  # ?????
end
Run Code Online (Sandbox Code Playgroud)

在BI中有另一种具有自己逻辑的视图,这并不重要.

我想在点击按钮时打开B. 我应该怎么做呢?

对于那些有iOS经验的人来说,这一定是显而易见的,但是我找不到你应该怎么做.任何指针都表示赞赏.Objectve-C中的解决方案是可以接受的,即使我更喜欢使用RubyMotion,也可以获得我的支持.

vac*_*ama 7

以下是使用模态视图控制器的方法:

app_delegate.rb:

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @window.rootViewController = MyViewA.alloc.init
    @window.makeKeyAndVisible
    true
  end
end
Run Code Online (Sandbox Code Playgroud)

viewa.rb:

class MyViewA < UIViewController

  def viewDidLoad
    super
    button = UIButton.buttonWithType UIButtonTypeRoundedRect
    button.setTitle "Open B", forState: UIControlStateNormal
    button.frame = [[10, 50], [300, 50]]
    button.addTarget(self, action: "open_b", forControlEvents: UIControlEventTouchUpInside)
    self.view.addSubview button
  end

  def open_b
    view_b = MyViewB.alloc.init
    view_b.delegate = self
    self.presentViewController view_b, animated:true, completion:nil
  end

  def done_with_b
    self.dismissViewControllerAnimated true, completion:nil
  end

end
Run Code Online (Sandbox Code Playgroud)

viewb.rb:

class MyViewB < UIViewController

  attr_accessor :delegate

  def viewDidLoad
    super
    button = UIButton.buttonWithType UIButtonTypeRoundedRect
    button.setTitle "Return to A", forState: UIControlStateNormal
    button.frame = [[10, 50], [300, 50]]
    button.addTarget(self, action: "press_button", forControlEvents: UIControlEventTouchUpInside)
    self.view.addSubview button
  end

  def press_button
    delegate.done_with_b
  end

end
Run Code Online (Sandbox Code Playgroud)