xcode 如何隐藏特定文本字段输入的标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34904059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:26:53  来源:igfitidea点击:

How to hide a label for specific text field input?

iosxcodeswift

提问by J.Vongehr

I′ve created a Xcode project with just one button, one text field and one label. I want to make the label only visible, when the text field input is "test123" or the button gets pressed. The part with the button works well, but no matter what i type in the textfield, the label stays hidden. Anyone help? This is my current code:

我创建了一个只有一个按钮、一个文本字段和一个标签的 Xcode 项目。我想让标签仅在文本字段输入为“test123”或按钮被按下时可见。带有按钮的部分运行良好,但无论我在文本字段中输入什么,标签都会保持隐藏状态。有人帮忙吗?这是我当前的代码:

    import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var Label: UILabel!
    @IBOutlet weak var Examplefield: UITextField!


    override func viewDidLoad() {

               super.viewDidLoad()
               // Do any additional setup after loading the view, typically from a nib.

               Label.hidden = true

        Examplefield.resignFirstResponder()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func ButtonPressed(sender: UIButton) {

        Label.hidden = false
    }

    func textFieldDidEndEditing(textField: UITextField) {
        if Examplefield.text == "test123" {
            Label.hidden = false

    }
        else {
            Label.hidden = true
        }

          }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }

}

回答by rob mayoff

The simplest solution is probably to connect the “Editing Changed” event of the text field to an action in your view controller:

最简单的解决方案可能是将文本字段的“Editing Changed”事件连接到视图控制器中的操作:

connecting the editing changed event

连接编辑更改事件

The text field fires the “Editing Changed” event whenever the user changes the contents of the field. You don't have to set the delegate or register for a notification.

每当用户更改字段内容时,文本字段都会触发“Editing Changed”事件。您不必设置委托或注册通知。

There are also “Editing Did End” and “Did End on Exit” events if you want to wait until the user exits the field.

如果您想等到用户退出该字段,还有“Editing Did End”和“Did End on Exit”事件。

回答by Mário Cosme

You need to implement the shouldChangeCharactersInRangemethod from the UITextFieldDelegatein order to check if the string is the same when the text changes:

您需要从UITextFieldDelegate实现shouldChangeCharactersInRange方法,以便在文本更改时检查字符串是否相同:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if (string == "test123") {
        self.Label.hidden = false
    } else {
        self.Label.hidden = true
    }
    return true
}

回答by g.Rapido

Using shouldChangeCharactersInRangeworks well, but another option would be using the textFieldDidChangeNSNotification.

使用shouldChangeCharactersInRange效果很好,但另一种选择是使用textFieldDidChangeNSNotification。

In your viewDidLoad, register for the notification:

在您的 中viewDidLoad,注册通知:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textFieldDidChange:"), name: "UITextFieldTextDidChangeNotification", object: nil)

Then define the appropriate method:

然后定义合适的方法:

 func textFieldDidChange(notification: NSNotification) {
    self.label.hidden = (self.textField.text == "test123")
}

回答by prajakta patil

Use UITextField Delegate method

使用 UITextField 委托方法

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    // your condition
}

enter image description here

在此处输入图片说明