1

假设我有 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

在 BI 中有另一种观点,有自己的逻辑,这并不重要。

我想在单击按钮时打开 B。我应该怎么去做?

对于具有一定 iOS 经验的任何人来说,这一定是显而易见的,但我找不到你应该怎么做。任何指针表示赞赏。Objectve-C 中的解决方案是可以接受的,并且会得到我的支持,即使我更希望它与 RubyMotion 一起使用。

4

2 回答 2

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

视图a.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

视图b.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
于 2012-11-04T17:01:20.667 回答
2

这是有关如何执行此操作的示例:https ://github.com/IconoclastLabs/rubymotion_cookbook/tree/master/ch_2/11_navbarbuttons

具体来说,您的方法将使用这部分:

def performAdd
    @secondary_controller = SecondaryController.alloc.init
    self.navigationController.pushViewController(@secondary_controller, animated:'YES')
end

每当您需要一些基础知识时,我强烈建议您参考这个 repo(是的,它是我的)!

http://iconoclastlabs.github.com/rubymotion_cookbook/

希望对你有用!

于 2012-11-04T15:32:52.413 回答