iPhone如何获取UITextField';在打字的时候是文本吗?

iPhone如何获取UITextField';在打字的时候是文本吗?,iphone,objective-c,ios,uitextfield,Iphone,Objective C,Ios,Uitextfield,我试图在一个单独的ui标签上显示对UITextField所做的更改。是否有办法在用户键入的每个字符后捕获UITextField的全文?目前我正在使用此方法,但它不会捕获用户输入的最后一个字符 我知道UITextView有“didChange”方法,但我找不到UITextField的方法 //does not capture the last character -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInR

我试图在一个单独的
ui标签上显示对
UITextField
所做的更改。是否有办法在用户键入的每个字符后捕获
UITextField
的全文?目前我正在使用此方法,但它不会捕获用户输入的最后一个字符

我知道
UITextView
有“didChange”方法,但我找不到
UITextField
的方法

//does not capture the last character

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

        [self updateTextLabelsWithText: textField.text];

    return YES;
}
输入每个字符后,如何从UITextField中提取文本?

谢谢大家!

  • 首先将一个
    UITextField
    UILabel
    添加到故事板/nib
  • 现在为
    UILabel
    分配
    IBOutlet
    (这里我使用了myLabel)
  • UITextFieldDelegate
    分配给文件所有者,并在.h文件中实现相同的委托
  • 使用以下代码行:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        [self updateTextLabelsWithText: newString];
    
        return YES;
    }
    
    -(void)updateTextLabelsWithText:(NSString *)string
    {
         [myLabel setText:string];
    }
    
  • 希望这有帮助。

    只需处理“编辑已更改”事件即可

    和选择器:

    -(void) editingChanged:(id)sender {
       // your code
    }
    

    您可以手动执行此操作,也可以通过按住CTRL键将“编辑已更改”发送事件拖动到.h来使用情节提要,为您创建编辑已更改的方法。

    在Swift 2.0+

     class CheckInViewController: UIViewController, UITextFieldDelegate {
    
     override func viewDidLoad() {
        super.viewDidLoad()
    
        yourTextField.delegate = self
    
     }
    
     func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
        var updatedTextString : NSString = textField.text as NSString
        updatedTextString = updatedTextString.stringByReplacingCharactersInRange(range, withString: string)
    
        self.methodYouWantToCallWithUpdatedString(updatedTextString)
        return true
     }
    
    }
    

    希望通过addTarget swift 2.0帮助您成为swift先锋+

       titleTextField.addTarget(self, action: #selector(textFieldTyping), forControlEvents: .EditingChanged)
    
    并执行
    选择器

    func textFieldTyping(textField:UITextField)
    {
        //Typing
    }
    

    在Swift 3.0中:

    其中一些解决方案落后一个字符,或用于Swift和Obj-C的早期版本。以下是我在Swift 3.0中使用的解决方案

    在类中,我声明了一个占位符来存储文本

    var tempName = ""
    
    在ViewDidLoad中,我使用了:

    nameField.addTarget(self, action: #selector(typingName), for: .editingChanged)
    
    然后我做了一个函数,叫做:

       func typingName(textField:UITextField){
    
            if let typedText = textField.text {
                tempName = typedText
                print(tempName)
            }
        }
    

    Swift 3.0+:这对我来说非常好

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        self.yourLabel.text = "\(textField.text ?? "")\(string)"
        return true
    }
    

    检查:这实际上是一个更好的解决方案。谢谢当您从文本字段中删除最后一个字符时,接受的答案将不会完全清除标签文本。如果我逐个删除字符,则应选择此作为答案。此方法不调用,如果用户返回或单击backspace怎么办?
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        self.yourLabel.text = "\(textField.text ?? "")\(string)"
        return true
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            if textField == yourTextFieldOutletName
            {
               if yourTextFieldOutletName.isEmpty == false
               {
                 youtlabelname.text = yourTextFieldOutletName.text!
                }
    
    
            }
    
            return true
    
        }
    
    func textViewDidEndEditing(_ textView: UITextView) {
           if textField == yourTextFieldOutletName
                {
                   if yourTextFieldOutletName.isEmpty == false
                   {
                     youtlabelname.text = yourTextFieldOutletName.text!
                    }
    
    
                } 
        }