如何在我的 UIViewController class 中声明 UIImageView 以便其他 classes 可以使用它?
how to declare UIImageView in my UIViewController class so that other classes can use it?
I will use GCD to run tasks that will in turn send tasks, that use wrist_band_UIImageView, to GCD main to display.
IE.
1. ViewController starts backend task onto DispatchQueue.global which is an endless loop
2. this endless loop periodically needs to update the UIImageView and so puts a task onto main queue to update the UIImageView.
class ViewController: UIViewController { <<<<<< E R R O R Class 'ViewController' has no initializers
@IBOutlet weak var app_title: UILabel!
//////////////////////// VARIABLES ////////////////////////////
var wrist_band_UIImageView: UIImageView
override func viewDidLoad() ///////////////////////////////////////// VIEW DID LOAD
{
print("viewDidLoad...")
super.viewDidLoad()
// Init data:
let image = UIImage(systemName: "applewatch")
self.wrist_band_UIImageView = UIImageView(image: image!)
print("viewDidLoad starts run_app.")
DispatchQueue.global().async{ app_class.run_app() }
print("viewDidLoad done.")
return
}
}
更新:
我将 UIImageView 从静态更改为实例,现在 class 行出现构建错误“Class 'ViewController' 没有初始化程序”。
我发现这个例子可以解决问题......
Class ViewController: UIViewController {
let someImageView: UIImageView = {
let theImageView = UIImageView()
theImageView.image = UIImage(named: "yourImage.png")
theImageView.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
return theImageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(someImageView) //This add it the view controller without constraints
someImageViewConstraints() //This function is outside the viewDidLoad function that controls the constraints
}
// do not forget the `.isActive = true` after every constraint
func someImageViewConstraints() {
someImageView.widthAnchor.constraint(equalToConstant: 180).isActive = true
someImageView.heightAnchor.constraint(equalToConstant: 180).isActive = true
someImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
someImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 28).isActive = true
}
}
I will use GCD to run tasks that will in turn send tasks, that use wrist_band_UIImageView, to GCD main to display.
IE.
1. ViewController starts backend task onto DispatchQueue.global which is an endless loop
2. this endless loop periodically needs to update the UIImageView and so puts a task onto main queue to update the UIImageView.
class ViewController: UIViewController { <<<<<< E R R O R Class 'ViewController' has no initializers
@IBOutlet weak var app_title: UILabel!
//////////////////////// VARIABLES ////////////////////////////
var wrist_band_UIImageView: UIImageView
override func viewDidLoad() ///////////////////////////////////////// VIEW DID LOAD
{
print("viewDidLoad...")
super.viewDidLoad()
// Init data:
let image = UIImage(systemName: "applewatch")
self.wrist_band_UIImageView = UIImageView(image: image!)
print("viewDidLoad starts run_app.")
DispatchQueue.global().async{ app_class.run_app() }
print("viewDidLoad done.")
return
}
}
更新:
我将 UIImageView 从静态更改为实例,现在 class 行出现构建错误“Class 'ViewController' 没有初始化程序”。
我发现这个例子可以解决问题......
Class ViewController: UIViewController {
let someImageView: UIImageView = {
let theImageView = UIImageView()
theImageView.image = UIImage(named: "yourImage.png")
theImageView.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
return theImageView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(someImageView) //This add it the view controller without constraints
someImageViewConstraints() //This function is outside the viewDidLoad function that controls the constraints
}
// do not forget the `.isActive = true` after every constraint
func someImageViewConstraints() {
someImageView.widthAnchor.constraint(equalToConstant: 180).isActive = true
someImageView.heightAnchor.constraint(equalToConstant: 180).isActive = true
someImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
someImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 28).isActive = true
}
}