![](/img/trans.png)
[英]Using `textField:shouldChangeCharactersInRange:`, how do i found that characters are mismatch?
[英]Using `textField:shouldChangeCharactersInRange:`, how do I get the text including the current typed character?
我正在使用下面的代碼嘗試更新textField2
的文本內容以匹配textField1
每當用戶輸入textField1
。
- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
if (theTextField == textField1){
[textField2 setText:[textField1 text]];
}
}
但是,我觀察到的輸出是......
textField2 為“12”,當 textField1 為“123”時
textField2 為“123”,當 textField1 為“1234”時
...當我想要的是:
textField2 為“123”,當 textField1 為“123”時
當 textField1 為“1234”時,textField2 為“1234”
我究竟做錯了什么?
-shouldChangeCharactersInRange
在文本字段實際更改其文本之前被調用,這就是您獲得舊文本值的原因。 要在更新后獲取文本,請使用:
[textField2 setText:[textField1.text stringByReplacingCharactersInRange:range withString:string]];
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(@"%@",searchStr);
return YES;
}
根據接受的答案,以下內容應該適用於Swift 3 :
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
return true
}
String
和NSString
都有稱為replacingCharacters:inRange:withString
。 然而,正如預期的那樣,前者需要一個Range
實例,而后者需要一個NSRange
實例。 textField
委托方法使用NSRange
實例,因此在這種情況下使用NSString
。
不要使用 UITextFieldDelegate,而是嘗試使用 UITextField 的“Editing Changed”事件。
在 Swift(4) 中,沒有NSString
(純 Swift):
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let textFieldString = textField.text, let swtRange = Range(range, in: textFieldString) {
let fullString = textFieldString.replacingCharacters(in: swtRange, with: string)
print("FullString: \(fullString)")
}
return true
}
作為擴展:
extension UITextField {
func fullTextWith(range: NSRange, replacementString: String) -> String? {
if let fullSearchString = self.text, let swtRange = Range(range, in: fullSearchString) {
return fullSearchString.replacingCharacters(in: swtRange, with: replacementString)
}
return nil
}
}
// Usage:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let textFieldString = textField.fullTextWith(range: range, replacementString: string) {
print("FullString: \(textFieldString)")
}
return true
}
Swift 版本:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if string == " " {
return false
}
let userEnteredString = textField.text
var newString = (userEnteredString! as NSString).stringByReplacingCharactersInRange(range, withString: string) as NSString
print(newString)
return true
}
這是你需要的代碼
if ([textField isEqual:self.textField1])
textField2.text = [textField1.text stringByReplacingCharactersInRange:range withString:string];
使用警衛
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
guard case let textFieldString as NSString = textField.text where
textFieldString.stringByReplacingCharactersInRange(range, withString: string).length <= maxLength else {
return false
}
return true
}
我的解決方案是使用UITextFieldTextDidChangeNotification
。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(copyText:) name:UITextFieldTextDidChangeNotification object:nil];
不要忘記調用[[NSNotificationCenter defaultCenter] removeObserver:self];
在dealloc
方法中。
如果您需要用這個替換文本字段文本,您可以使用我的解決方案(Swift 3): https : //gist.github.com/Blackjacx/2198d86442ec9b9b05c0801f4e392047
替換后,您只需獲取textField.text
即可檢索組合文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.