【问题标题】:How to use Next Previous buttons to go to next field如何使用下一个上一个按钮转到下一个字段
【发布时间】:2013-02-01 01:52:29
【问题描述】:

我设置了一个包含两个字段的视图控制器。我在键盘上方实现了一个工具栏,其中包含上一个和下一个按钮。

我有一个字段列表的数组。

    // Setup array of listed fields
textFields = [[NSArray alloc] initWithObjects:usernameField,passwordField, nil];

工具栏

(id)initWithTextField:(UITextField*)textField
{
    self = [super initWithFrame:CGRectMake(0, 250, 320, 40)];
    if (self) {

        inputToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
        doneButton= [[UIBarButtonItem alloc]
                     initWithTitle:@"Done"
                     style:UIBarButtonItemStyleDone
                     target:self
                     action:@selector(performdone:)];
        nextButton= [[UIBarButtonItem alloc]
                     initWithTitle:@"Next"
                     style:UIBarButtonItemStyleBordered
                     target:self
                     action:@selector(nextButtonMethod:)];
        nextButton.tintColor = [UIColor blackColor];
        previousButton= [[UIBarButtonItem alloc]
                     initWithTitle:@"Previous"
                     style:UIBarButtonItemStyleBordered
                     target:self
                     action:@selector(previousButtonMethod:)];
        previousButton.tintColor = [UIColor blackColor];

        inputToolBar.barStyle=UIBarStyleDefault;
        UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

        [inputToolBar setItems:[NSArray arrayWithObjects:previousButton,nextButton,flexSpace,doneButton,Nil] animated:NO];
        [self addSubview:inputToolBar];
    }
    return self;
}

对工具栏的字段调用:

[self.usernameField setInputAccessoryView:myInputAccessoryView];

我想知道在我的 nextButtonMethod 和 previousButtonMethod 方法中添加什么以使光标转到数组列表中的下一个字段。我怎样才能做到这一点?

【问题讨论】:

    标签: ios ios5 uiviewcontroller uitextfield


    【解决方案1】:
    [textFieldInstance becomeFirstResponder];
    

    将光标放在该字段上。希望您知道如何选择正确的文本字段实例。

    【讨论】:

      【解决方案2】:

      以下框架可能会对您的情况有所帮助..

      https://github.com/wzbozon/DKKeyboardView 要么 https://github.com/SimonBS/BSKeyboardControls

      非常简单的。

      【讨论】:

        【解决方案3】:

        所以我可能会推荐的一件事是设置一个视图控制器,创建一个 xib 并使用 UITextField 的内置 setInputAcessoryView。

        在我制作的示例中,我使用 xib 制作了一个自定义视图控制器,并将其设置为我希望按钮的外观,然后将该视图加载到 InputAccessoryView 通过以这种方式加载它,您可以让按钮在它辞职或成为第一响应者时通过键盘上下动画。

        -(void)createFunction{
        
          [myCustomTextField setDelegate:self];
          [myCustomTextField setInputView:nil]; 
          //Don't have to set this unless you have a custom view
          [myCustomTextField setInputAccessoryView:myCustomInputButtonViewController.view];
        
        }
        

        然后我从我的 AccessoryView 视图控制器设置委托方法。这样当按钮被按下时,我可以得到输入并做出相应的反应。

        -(void)pressedLeftActionButtonFromEXT{
        NSLog(@"Pressed Left Action");
        NSInteger nextTag = activeTextField.tag - 1;
        // Try to find next responder
        UIResponder* nextResponder = [activeTextField.superview viewWithTag:nextTag];
        if (nextResponder) {
            // Found next responder, so set it.
            [nextResponder becomeFirstResponder];
          } else {
            // Not found, so remove keyboard.
            [activeTextField resignFirstResponder];
          }
        
        }
        

        这是我创建的示例项目中的确切代码行。正如你可以想象的那样,另一个方向正好相反。

        我的界面中还有一个 UITextField * activeTextField,这样我就可以知道我何时打开了 textField 并做出相应的响应。

        我希望这会有所帮助。如果这不是很让我知道,我会尝试更好地澄清。我知道我可以解决你遇到的这个问题。

        好汉^^

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-15
          • 1970-01-01
          • 2013-02-14
          • 1970-01-01
          • 1970-01-01
          • 2021-08-07
          • 1970-01-01
          • 2011-08-13
          相关资源
          最近更新 更多