0

我正在尝试使用简单的控件创建血压应用程序以添加结果。为了简单起见,我决定当用户在收缩压中键入 3 个字符时,应用程序应将光标(和第一响应者)移动到舒张压值,然后再移动到脉搏。所以我是这样制作的:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([self systolicValue].isFirstResponder && [self systolicValue].text.length == 3) {
        [[self diastolicValue] becomeFirstResponder];
    }
    else if ([self diastolicValue].isFirstResponder && [self diastolicValue].text.length == 2) {
        [[self pulseValue] becomeFirstResponder];
    }
    else if ([self pulseValue].isFirstResponder && [self pulseValue].text.length == 2) {
        [[self pulseValue] resignFirstResponder];
    }
    return YES;
}

我相信这是一个很好的解决方案,但不能以我想要的方式工作。systolicValue 中的三个数字不会立即使 diastolicValue 成为第一响应者。第一个用户必须按另一个数字,然后光标移动到下一个文本字段,数字就在这里。这有点不方便,因为用户不知道光标在三个数字后会自动移动。

知道如何立即关注下一个文本字段吗?我也尝试[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];viewDidLoad(), 和类似于上面的方法(但return当然没有,因为它是void),但它也不起作用。事实上它根本不起作用,在第一个数字之后:

2013-08-07 08:39:09.805 MyApp[973:c07] -[AddViewController textFieldTextDidChange:]: unrecognized selector sent to instance 0x7580000
2013-08-07 08:39:09.809 MyApp[973:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AddViewController textFieldTextDidChange:]: unrecognized selector sent to instance 0x7580000'
*** First throw call stack:
(0x1c92012 0x10cfe7e 0x1d1d4bd 0x1c81bbc 0x1c8194e 0xb904f9 0x1cec0c5 0x1c46efa 0xac4bb2 0xad7e6b 0xe5174 0xef5a6 0xb904f9 0x1cec0c5 0x1c46efa 0xac4bb2 0x35689de 0x26b61da 0x2aefdfc 0x2af2bf8 0x3543612 0x354374a 0x3543ec0 0x3543cb8 0x3543204 0x293122b 0x2931193 0x3515e96 0x35424cc 0x2aed136 0x2aec3c6 0x2b1f980 0x317b7fd 0x2b16576 0x2b176da 0x2b1572e 0x3179eaa 0x2b2faf1 0x2b1f72a 0x2af36ae 0x26f262b 0x10e36b0 0x355e810 0x27311a4 0x27332ff 0x1d50b4 0x197aef 0x198e58 0x1979fe 0x1a1d29 0x24ddb 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x1227f5 0x24e35 0x24806 0x24beb 0x16698 0x1beddf9 0x1bedad0 0x1c07bf5 0x1c07962 0x1c38bb6 0x1c37f44 0x1c37e1b 0x1bec7e3 0x1bec668 0x13ffc 0x1f2d 0x1e55 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 
4

1 回答 1

1

尝试做这样的事情:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  // return NO to not change text
{

    NSMutableString *finalString = [[NSMutableString alloc] initWithString:textField.text];
    [finalString replaceCharactersInRange:range withString:string];


    if ([self systolicValue].isFirstResponder && [self systolicValue].text.length == 3))
    {
        [[self systolicValue] setText:finalString];
        [[self systolicValue] resignFirstResponder];
        return NO;
    }

    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField          // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
{
    [[self diastolicValue] becomeFirstResponder];
}
于 2013-08-07T07:19:03.820 回答