我正在使用下面的代码尝试textField2让textField1用户输入时更新文本内容以匹配textField1.
- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
if (theTextField == textField1){
[textField2 setText:[textField1 text]];
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我观察到的输出是......
当textField1为"123"时,textField2为"12"
当textField1为"1234"时,textField2为"123"
......当我想要的是:
当textField1为"123"时,textField2为"123"
当textField1为"1234"时,textField2为"1234"
我究竟做错了什么?
据说Objective-C接受"\ b"作为退格的特殊字符,如何在程序中捕获它?
我的目的是在if语句中捕获它,以便为我的textField启用屏幕字符:
if([someCharacter isEqualToString:@"\ b"]){}
旧SDK解决方案:
- (void)modifyKeyboard:(NSNotification *)notification
{
UIView *firstResponder = [[[UIApplication sharedApplication] keyWindow] performSelector:@selector(firstResponder)];
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
for (UIView *keyboard in [keyboardWindow subviews])
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
{
MyFancyKeyboardView *customKeyboard = [[MyFancyKeyboardView alloc] initWithFrame: CGRectMake(0, 0, keyboard.frame.size.width, keyboard.frame.size.height);
[keyboard addSubview: customKeyboard];
[customKeyboard release];
}
}
Run Code Online (Sandbox Code Playgroud)
按照上面的方法,我现在发现iOS4是不同的.不起作用.我确信这是由于命名子视图(或排序)的差异.对于iphone SDK 4,有谁知道如何解决同样的问题?