У меня есть пользовательский UIControl (TestControl) и вы хотите передать простую строку метке на главном экране. UIControl (TestControl) находится внутри UIView (CustomView), который был размещен на раскадровке, используя представление, которое имеет пользовательский класс (CustomView).Передача данных между настраиваемым контроллером представления и просмотра
Что было бы простой реализацией, которая позаботилась об этом?
Я заметил, что CustomView вызывается перед viewDidLoad() в ViewController.
ViewController.swift
import UIKit
@IBOutlet var someLabel: UILabel!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
TestControl.swift
import UIKit
class TestControl: UIControl {
// Initializer
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.blackColor()
//this is where it would be good if a string could be passed to a label
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
CustomView.swift
import UIKit
@IBDesignable class CustomView: UIView{
#if TARGET_INTERFACE_BUILDER
override func willMoveToSuperview(newSuperview: UIView?) {
let testing: TestControl = TestControl(frame: self.bounds)
self.addSubview(testing)
}
#else
override func awakeFromNib() {
super.awakeFromNib()
let testing: TestControl = TestControl(frame: self.bounds)
self.addSubview(testing)
}
#endif
}
нужен код anyways –