以下是使用模态视图控制器的方法:
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