如果TextField为空,我需要禁用make按钮。否则它会崩溃我的应用
(主题1:致命错误:在解开可选值时意外发现nil )。
我知道,这里人们写了很多次。但我尝试了StackOverflow的许多例子,如:
if (MyTextField.text?.isEmpty)! {
MyButton.isEnabled = false
MyButton.alpha = 0.5
}
上面的代码我放入了viewDidLoad并且它没有用。 如果我按下按钮:
@IBAction func acceptButton(_ sender: Any) {
if (MyTextField.text?.isEmpty)! {
MyButton.isEnabled = false
MyButton.alpha = 0.5
...
然后按钮始终禁用。甚至我在TextField中放了一些数字。
下面的代码也不起作用:
override func viewDidLoad() {
super.viewDidLoad()
MyTextField.addTarget(self, action: #selector(actionTextFieldIsEditingChanged), for: UIControlEvents.editingChanged)
...
}
...
@objc func actionTextFieldIsEditingChanged(sender: UITextField) {
if sender.MyTextField.isEmpty {
MyButton.isEnabled = false
MyButton.alpha = 0.5
} else {
MyButton.isEnabled = true
MyButton.alpha = 1.0
}
}
我不能使用的其他部分代码,因为它是从2014 - 2015年开始的。
答案 0 :(得分:1)
You need to implement UITextFieldDelegate
method to enable button when there is text in text filed.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
myTextField.delegate = self
}
}
Implement this delegate method...
extension ViewController: UITextFieldDelegate {
func textFieldDidEndEditing(_ textField: UITextField) {
if let text = textField.text, text.isEmpty {
MyButton.isUserInteractionEnabled = false
MyButton.isEnabled = false
MyButton.alpha = 0.5
} else {
MyButton.isUserInteractionEnabled = true
MyButton.isEnabled = true
MyButton.alpha = 1
}
}
}
You can disable button in viewDidLoad method as well.
if let text = MyTextField.text, text.isEmpty {
MyButton.isUserInteractionEnabled = false
MyButton.isEnabled = false
MyButton.alpha = 0.5
}
EDIT
You can also enable and disable button as user types in textfield.
extension ViewController: UITextFieldDelegate {
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let text = (textField.text! as NSString).replacingCharacters(in: range, with: string)
if text.isEmpty{
MyButton.isUserInteractionEnabled = false
MyButton.isEnabled = false
MyButton.alpha = 0.5
} else {
MyButton.isUserInteractionEnabled = true
MyButton.isEnabled = true
MyButton.alpha = 1
}
return true
}
}
答案 1 :(得分:1)
第一组
override func viewDidLoad()
{
super.viewDidLoad()
btn.isEnabled = false
}
然后使用文本字段委托方法
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print(textField.text?.count ?? "default")
if textField == MyTextField
{
if MyTextField.text?.count == 1 && string == ""
{
MyButton.isEnabled = false
}
else
{
MyButton.isEnabled = true
}
}
return true
}
答案 2 :(得分:0)
@IBAction func acceptButton(_ sender: Any) {
if (MyTextField.text?.isEmpty)! {
print("i am not doing anything")
}
else{
//perform the code
}
}
答案 3 :(得分:0)
为此使用UITextFieldDelegate
方法shouldChangeCharactersIn
。
首先将您的课程与UITextFieldDelegate
这样绑定
class ClassName: UIViewController, UITextFieldDelegate { ...
然后将此代码添加到您的viewDidLoad
myTextField?.delegate = self
MyButton?.isUserInteractionEnabled = false
MyButton?.alpha = 0.5
并实现此委托方法
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text = (textField.text! as NSString).replacingCharacters(in: range, with: string)
if !text.isEmpty{
MyButton?.isUserInteractionEnabled = true
MyButton?.alpha = 1.0
} else {
MyButton?.isUserInteractionEnabled = false
MyButton?.alpha = 0.5
}
return true
}
答案 4 :(得分:0)
我没有看到完整的答案,所以去了:
MyButton
更改为myButton
,并将MyTextField
更改为myTextField
。isUserInteractionEnabled
和alpha
。只需设置按钮的isEnabled
标志,然后让UIKit决定如何绘制禁用的按钮即可。这样,您就可以按照正常的HIG约定显示禁用按钮。viewWillAppear(_:)
函数添加代码以禁用该按钮。赞:
override func viewWillAppear(_ animated: Bool) {
myButton.isEnabled = (myTextField.text?.count ?? 0) > 0
}
使您的视图控制器符合UITextFieldDelegate
协议。然后,将控件从文本字段拖放到视图控制器上,以连接委托链接。
如果文本字段为空,则向视图控制器添加textField(_:shouldChangeCharactersIn:replacementString:)
函数以禁用按钮。请注意,此函数必须确定替换完成后文本字段是否为空,因此它需要计算替换字符串后字段文本的长度。您可以将UITextFieldDelegate
函数添加到视图控制器的扩展中,以很好地将它们分开。
赞:
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
//Make sure this method is being called
guard textField == myTextField else { return true }
let newLength = (textField.text?.count ?? 0) - range.length + string.count
myButton.isEnabled = newLength > 0
return true
}
}