Let's say I have 2 controllers A and B.
In A I have:
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
In B I have another view with its own logic, which is not important.
I want to open B when clicking the button. How should I go and do this?
This must be dead obvious for anyone with some iOS experience, but I can't find how you're supposed to do it. Any pointers are appreciated. A solution in Objectve-C is acceptable and would get my upvote, even if I'd prefer it to be with RubyMotion.
Here is how to do it using a modal view controller:
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