UItextfields之间的UIToolbar导航

时间:2015-07-28 19:32:01

标签: ios objective-c iphone

我在我的应用中设置了这个 enter image description here

我试图在文本字段与uibarbutton之间进行更改,但它不起作用

我有这个代码

-(void)setToolBar {
    UIToolbar* Toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];

    Toolbar.items = [NSArray arrayWithObjects:
                     [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                     [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backwardIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(prevTextField)],
                     [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"forwardIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField)], nil];
    [Toolbar sizeToFit];

    self.txtNombre.inputAccessoryView = Toolbar;
    self.txtAM.inputAccessoryView = Toolbar;
    self.txtAP.inputAccessoryView = Toolbar;
    self.txtEmail.inputAccessoryView = Toolbar;
    self.txtPass.inputAccessoryView = Toolbar;
}

    -(void)nextTextField {
    if(self.txtNombre){
        [self.txtNombre resignFirstResponder];
        [self.txtAP becomeFirstResponder];
    }else if(self.txtAP){
        [self.txtAP resignFirstResponder];
        [self.txtAM becomeFirstResponder];
    }else if(self.txtAM){
        [self.txtAM resignFirstResponder];
        [self.txtTel becomeFirstResponder];
    }else if(self.txtTel){
        [self.txtTel resignFirstResponder];
        [self.txtEmail becomeFirstResponder];
    }else if(self.txtEmail){
        [self.txtEmail resignFirstResponder];
        [self.txtPass becomeFirstResponder];
    }else{
        [self.txtPass resignFirstResponder];
    }
}

-(void)prevTextField{
    if(self.txtAP){
        [self.txtNombre becomeFirstResponder];
        [self.txtAP resignFirstResponder];
    }else if(self.txtAM){
        [self.txtAP becomeFirstResponder];
        [self.txtAM resignFirstResponder];
    }else if(self.txtTel){
        [self.txtAM becomeFirstResponder];
        [self.txtTel resignFirstResponder];
    }else if(self.txtEmail){
        [self.txtTel becomeFirstResponder];
        [self.txtEmail resignFirstResponder];
    }else if(self.txtPass){
        [self.txtEmail becomeFirstResponder];
        [self.txtPass resignFirstResponder];
    }
}

关于我做错什么的任何想法,因为它只是在前两个文本字段之间发生变化

1 个答案:

答案 0 :(得分:4)

而不是检查:

if(self.txtNombre)

你应该检查

if([self.txtNombre isFirstResponder])

当然,对于所有文本字段。 那就是检查你的UITextField当前是否正在被编辑。

我还建议,从那些UITextFields制作NSArray,使这些方法看起来更好。现在,如果你想添加更多的文本字段,你需要复制粘贴很多并重命名属性 - 也很容易弄错。