标签标签0的奇怪问题?

时间:2012-01-31 19:41:26

标签: iphone objective-c ios xcode

我在键盘上创建了一个用于文本字段的栏,其中包含上一个/下一个/完成按钮选项。在这样做的过程中,我注意到我用于在文本字段之间导航的标签出现了奇怪的现象。我用循环编程创建我的接口,因此,只需将标记值设置为循环变量i。

我在0处启动了i变量,因此创建的第一个文本字段的标记为零。基本上发生的事情是“先前的”按钮功能只会低到1.它甚至不会返回带有0标签的文本字段。解决此问题的唯一方法是将所有标记值增加1,以便第一个文本字段从1开始而不是零。

这是我的代码。我的代码中是否有一个我看不到的错误?或者这是标签的奇怪问题?

    -(void)gotoPrevTextfield{
// If the active textfield is the first one, can't go to any previous
// field so just return.
 UITextField *textField =  (UITextField *)[inputsView viewWithTag:0];
NSLog(@"%i",textField.tag);
NSLog(@"%i",txtActiveField.tag);
if (txtActiveField == textField) {
    NSLog(@"returning at previous");
    return;
}
else {
    NSLog(@"set responder");
    // Otherwise if a different textfield has the focus, the operation
    // of "previous" button can be done and set the previous as the first
    // responder.
    textField = (UITextField *)[inputsView viewWithTag:txtActiveField.tag - 1];
    NSLog(@"%i",textField.tag);
    NSLog(@"%i",txtActiveField.tag);
    [textField becomeFirstResponder];
}

}

1 个答案:

答案 0 :(得分:2)

请注意,unset标签默认为0,因此这几乎是一个糟糕的选择。你可能会得到另一种你不期望的观点。

一个相当不错的做法是添加一些常量,例如100,考虑使常量为const int或#define以便清晰。

相关问题