我有一个 UITextField,我想添加一个“?”所有输入文本的后缀。
用户不应该能够删除这个“?”或在其右侧添加文本。
解决这个问题的最佳方法是什么?
Use the UITextFieldDelegate http://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/每当编辑字段时更改字符串的协议。这是一个快速的尝试;这需要一些工作,但它应该可以帮助您开始。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * currentText = [textField text];
if( [currentText characterAtIndex:[currentText length] - 1] != '?' ){
NSMutableString * newText = [NSMutableString stringWithString:currentText];
[newText replaceCharactersInRange:range withString:string];
[newText appendString:@"?"];
[textField setText:newText];
// We've already made the replacement
return NO;
}
// Allow the text field to handle the replacement
return YES;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)