【发布时间】:2016-06-10 02:15:51
【问题描述】:
我的设置很简单:
- 我有一个
UITextField(输入视图是一个pickerView) - 我有一个
UITextView - 我有一个
UIButton
如果 文本字段和文本视图都为空,我只想禁用该按钮。并在它们都包含某些内容时启用它。
我尝试的任何方法似乎都不起作用。
我用这个创建我的按钮:
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(self.view.frame.width, self.view.frame.height - 90, self.view.frame.width - 60, 50)
button.center.x = self.view.frame.width / 2
button.backgroundColor = UIColor.clearColor()
button.layer.cornerRadius = 25
button.layer.borderWidth = 1
button.layer.borderColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1).CGColor
button.tintColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
button.setTitle("Send email", forState: UIControlState.Normal)
button.titleLabel!.font = UIFont(name: "Typo GeoSlab Regular Demo", size: 15)
button.addTarget(self, action: "sendEmail:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
对于文本字段的检查,我已经尝试过:
if subjectTextField.text!.isEmpty || bodyTextView.text.isEmpty {
button.userInteractionEnabled = false
} else {
button.userInteractionEnabled = true
}
我也试过这个:
if subjectTextField.text == "" || bodyTextView.text == "" {
button.userInteractionEnabled = false
} else {
button.userInteractionEnabled = true
}
我已经在viewDidLoad() 方法中添加了这段代码,也尝试在textFieldDidEndEditing 方法中添加它。两者都没有任何区别。
始终按钮保持启用状态。请帮忙! 谢谢,如果您需要我提供更多信息,请告诉我。
【问题讨论】:
-
尝试检查文本字段和文本视图中的文本长度... subjectTextField.text.length==0 || bodyTextView.text.length==0
标签: ios swift uibutton uitextfield uitextview