我目前要显示性别:
var user: User? {
didSet {
nameTextField.text = user?.username
genderLabel.text = user?.gender
}
}
我只知道显示文本标签,如何根据用户(当前用户和其他用户的个人资料)性别显示“男性图标”或“女性图标”?
答案 0 :(得分:-1)
您没有提供足够的上下文,但是,我将基于四个假设尝试为您提供帮助:
user
对象设置初始值,因为属性观察者需要设置初始值didSet
不会触发例如,如果您有一个名为“ genderImageView”之类的imageView,则可以在此属性观察器中进行设置,如下所示:
var user: User? {
didSet{
nameTextField.text = user?.username
genderLabel.text = user?.gender
var image = UIImage()
if user?.gender == "male" { // for example
image = UIImage(named: " name of male gender icon in asset catalog")
} else if user?.gender == "another gender string" {
image = UIImage(named: "name of this gender in your asset catalog")
} // more else-if blocks or else block here to support all genders you want
genderImageView.image = image
} // forgot your closing bracket here, or not showing enough information about this property observer
}