gpt4 book ai didi

iphone - 使用 RubyMotion 单击按钮时如何加载 Controller ?

转载 作者:太空宇宙 更新时间:2023-11-03 17:07:58 27 4
gpt4 key购买 nike

假设我有 2 个 Controller A 和 B。

在 A 中我有:

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

在 B 中我有另一个 View ,它有自己的逻辑,这并不重要。

我想在点击按钮的时候打开B。我应该怎么做?

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

最佳答案

以下是使用模态视图 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

关于iphone - 使用 RubyMotion 单击按钮时如何加载 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13219433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com