如何在Swift 4的视图定义中访问其他视图

时间:2018-09-26 19:02:06

标签: swift xcode autolayout swift4

当我想引用其他视图时,出现此错误:

  类型'((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

1 个答案:

答案 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
    }
}