[英]How to access UI objects from loaded storyboard view in swift playground?
I have got a storyboard loading in a playground. 我在操场上装了一个故事板。 It works as intended, but now I want to be able add playground code, but don't know how to access the objects in the view from the storyboard.
它可以按预期工作,但是现在我希望能够添加游乐场代码,但不知道如何从情节提要中访问视图中的对象。 How would I add the @IBOutput and @IBAction in the playground.
如何在操场上添加@IBOutput和@IBAction。 I have a UILabel called label & a UIButton called button.
我有一个名为label的UILabel和一个名为button的UIButton。
Here is the playground code. 这是游乐场代码。
import UIKit
import PlaygroundSupport
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() as! UIViewController
PlaygroundPage.current.liveView = vc
playground view screenshot 游乐场视图截图
Thx 谢谢
One way you could do it, is to set a unique tag for each of your objects. 一种方法是为每个对象设置唯一的标签。
yourLabel.tag = 10
for example. 例如,
yourLabel.tag = 10
。
Then you could access it with 然后您可以使用
var myLabel = self.view.viewWithTag(10) as? UILabel
For your button you would do: 对于您的按钮,您可以执行以下操作:
yourButton.tag = 11
var myButton = self.view.viewWithTag(11) as? UIButton
myButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
func buttonAction(_ sender: UIButton){
print("Button with tag \(sender.tag) tapped")
}
This code is untested, but you should be able to get the idea 此代码未经测试,但是您应该能够理解
Good luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.