2009-11-25 36 views

回答

-1

我刚刚遇到同样的问题。我正在从textFieldDidBeginEditing:call中更改属性。

将属性更改移至textFieldShouldBeginEditing:call修复了问题。

+0

我有一个像你说的尝试,但它没有工作... – cloosen 2012-09-18 08:51:50

0

保存在属性中输入的文本。例如:

NSString *_password; 

然后在委托:

textFieldDidBeginEditing:(UITextField *)textField 

分配textField.text = _password;

0

我子类的UITextField此。我有掩蔽的UITextField(用于打开安全入口和关闭)在屏幕上单独控制,这是一个更合适的地方为我解决这个问题:

@interface ZTTextField : UITextField { 
    BOOL _keyboardJustChanged; 
} 
@property (nonatomic) BOOL keyboardJustChanged; 
@end 

@implementation ZTTextField 
@synthesize keyboardJustChanged = _keyboardJustChanged; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     _keyboardJustChanged = NO; 
    } 
    return self; 
} 

- (void)insertText:(NSString *)text { 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 
    if (self.keyboardJustChanged == YES) { 
     BOOL isIOS7 = NO; 
     if ([[UIApplication sharedApplication] respondsToSelector:@selector(backgroundRefreshStatus)]) { 
      isIOS7 = YES; 
     } 
     NSString *currentText = [self text]; 
     // only mess with editing in iOS 7 when the field is masked, wherein our problem lies 
     if (isIOS7 == YES && self.secureTextEntry == YES && currentText != nil && [currentText length] > 0) { 
      NSString *newText = [currentText stringByAppendingString: text]; 
      [super insertText: newText]; 
     } else { 
      [super insertText:text]; 
     } 
     // now that we've handled it, set back to NO 
     self.keyboardJustChanged = NO; 
    } else { 
     [super insertText:text]; 
    } 
#else 
    [super insertText:text]; 
#endif 
} 

- (void)setKeyboardType:(UIKeyboardType)keyboardType { 
    [super setKeyboardType:keyboardType]; 
    [self setKeyboardJustChanged:YES]; 
} 

@end 
0

这个工作对我来说:为了返回FALSE禁止textview自行更改和设置更改。

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    NSString *newContent = [textField.text stringByReplacingCharactersInRange:range withString:string]; 

    [textField setText:newContent]; 
    return FALSE; 
}