当我想引用其他视图时,出现此错误:
类型'((LoginController)->()->(LoginController)'的值没有 成员'userNameTF'
这是我的代码:
class LoginController: UIViewController {
var userNameTF: UITextField = {
let uName = UITextField()
uName.translatesAutoresizingMaskIntoConstraints = false
uName.textAlignment = .right
uName.placeholder = "userName"
uName.textColor = .gray
uName.returnKeyType = .done
return uName
}()
private var contentView: MDCCard = {
let view = MDCCard()
view.translatesAutoresizingMaskIntoConstraints = false
view.cornerRadius = 0
view.backgroundColor = UIColor(hexString: "#F2F2F2")
view.setShadowElevation(ShadowElevation(rawValue: 6), for: .selected)
view.setShadowColor(UIColor.black, for: .highlighted)
view.addSubview(self.userNameTF)//here I call userNameTF}
}
我的问题是如何在userNameTF
内部调用contentView
?
答案 0 :(得分:0)
我更改了部分代码,它可以正常工作。我只是将您的自定义视图更改为UIView:
class LoginController: UIViewController {
var userNameTF: UITextField = {
let uName = UITextField()
uName.translatesAutoresizingMaskIntoConstraints = false
uName.textAlignment = .right
uName.placeholder = "userName"
uName.textColor = .gray
uName.returnKeyType = .done
return uName
}()
private var contentView: UIView {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .red
view.addSubview(self.userNameTF)
return view
}
}