Getting the Value of a UITextField as keystrokes are entered?

前端 未结 3 1784
醉酒成梦
醉酒成梦 2020-11-30 18:13

Let\'s say I have the following code:

IBOutlet UITextField* nameTextField;
IBOutlet UILabel* greetingLabel;

I\'d like the greetingLab

3条回答
  •  温柔的废话
    2020-11-30 18:26

    you could register an action for the event UIControlEventEditingChanges on the text field:

    [nameTextField addTarget:self action:@selector(updateLabelUsingContentsOfTextField:) forControlEvents:UIControlEventEditingChanged];
    

    ...

    // TODO: error checking
    
    - (void)updateLabelUsingContentsOfTextField:(id)sender {
    
        greetingLabel.text = [NSString stringWithFormat:@"Hello %@", ((UITextField *)sender).text];
    
    }
    

提交回复
热议问题