1

A と B の 2 つのコントローラーがあるとします。

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

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

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
于 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

基本が必要なときはいつでも、このレポを参照することを強くお勧めします (私のものです)。

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

それがあなたのためにそれをすることを願っています!

于 2012-11-04T15:32:52.413 に答える