1

我在键盘工具栏上有两个按钮,下一个按钮总是将最后一个字段设置为我的第一响应者,跳过所有中间的..加上这个键盘工具栏也没有显示在最后一个字段上。我有一个存储所有文本字段和文本视图对象的数组,点击下一个和上一个我使用这个数组来确定哪个 obj 将成为第一响应者,下面是我的代码。

这是我创建键盘工具栏的地方

-(void)createInputAccessoryView

 {
self.keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 44)];
self.keyboardToolbar.barStyle = UIBarStyleBlackOpaque;
self.keyboardToolbar.tintColor = [UIColor clearColor];

UIButton *previousButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[previousButton setTitle:@"Previous" forState:UIControlStateNormal ];
[previousButton setBackgroundColor:[UIColor clearColor]];
[previousButton addTarget:self action:@selector(gotoPrevTextfield) forControlEvents:UIControlStateNormal];

UIButton *nextButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[nextButton setTitle:@"Next" forState:UIControlStateNormal];
[nextButton setBackgroundColor:[UIColor clearColor]];
[nextButton addTarget:self action:@selector(gotoNextTextfield) forControlEvents:UIControlStateNormal];

//_btnPrev = [[UIBarButtonItem alloc] initWithCustomView:previousButton];

//_btnNext = [[UIBarButtonItem alloc] initWithCustomView:nextButton];

_btnPrev =[[UIBarButtonItem alloc]initWithTitle:@"Previous" style:UIBarButtonItemStylePlain target:self action:@selector(gotoPrevTextfield)];
_btnNext=[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(gotoNextTextfield)];

 [self.keyboardToolbar setItems:[NSArray arrayWithObjects:_btnPrev,_btnNext,nil] animated:NO];}

这是我创建对象数组的地方

-(void)addTextFieldObjectsToArray
 {
textFieldsArray = [[NSMutableArray alloc]initWithCapacity:22];

[textFieldsArray addObject:companyNameFieldObj];
[textFieldsArray addObject:contactPersonFieldObj];
[textFieldsArray addObject:customerPhoneFieldObj];
[textFieldsArray addObject:customerEmailFieldObj];
[textFieldsArray addObject:distributorNameFieldObj];
[textFieldsArray addObject:distributorSalespersonFieldObj];
[textFieldsArray addObject:distributorPhoneFieldObj];
[textFieldsArray addObject:distributorEmailFieldObj];
[textFieldsArray addObject:closetStandardFieldObj];
[textFieldsArray addObject:cylinderVelocityFieldObj];
[textFieldsArray addObject:cycleRateFieldObj];
[textFieldsArray addObject:operatingPressureFieldObj];
[textFieldsArray addObject:cylinderMountFieldObj];
[textFieldsArray addObject:wieghtOfRodAttachmentFieldObj];
[textFieldsArray addObject:rodAttachmentFieldObj];
[textFieldsArray addObject:sideAttchementFieldObj];
[textFieldsArray addObject:environmentConsiderationFieldObj];
[textFieldsArray addObject:ambientTempFieldObj];
[textFieldsArray addObject:quantityFieldObj];
[textFieldsArray addObject:competitorFieldObj];
[textFieldsArray addObject:currentApplicationIssuesFieldObj];
[textFieldsArray addObject:modificationRequiredTextViewObj];}

现在这是我的下一个和上一个按钮代码

-(void)gotoPrevTextfield{
for(int i=1 ;i<22;i++)
{

        if([textFieldsArray [i] isFirstResponder])
        {
            [textFieldsArray[i-1] becomeFirstResponder];
            [textFieldsArray [i] resignFirstResponder];

            break;
        }

}}

 -(void)gotoNextTextfield
 {
   NSLog(@"%s","inside next Field");

for( int i=0 ;i<21;i++)
{
    if([textFieldsArray [i] isFirstResponder])
    {
        [textFieldsArray [i+1] becomeFirstResponder];
        [textFieldsArray [i] resignFirstResponder];

        break;
    }

}}

现在,当我单击任何文本字段时,我对文本字段和文本视图都使用了 didbeginediting函数。 这是我的 func 代码

  - (void)textFieldDidBeginEditing:(UITextField *)textField
{
     _txtActiveField = textField;
     [self.txtActiveField setInputAccessoryView:self.keyboardToolbar];
  }}

- (void)textViewDidBeginEditing:(UITextView *)textView
{


    if ([textView isFirstResponder]) {

        _txtActiveView =textView;

        [self.txtActiveView setInputAccessoryView:self.keyboardToolbar];

    }}

最后一个对象是 textview,其他是文本字段。请帮助我了解为什么我的所有字段都被跳过并且我的最后一个字段被设置为单击下一个按钮时的第一响应者

4

2 回答 2

1

我对您的要求的最佳建议是BSKeyboardControls

请看一看

https://github.com/simonbs/BSKeyboardControls#installation

https://github.com/simonbs/BSKeyboardControls#usage

于 2013-11-05T09:37:17.670 回答
1

你的实现是错误的。因为 for 循环, -(void)gotoNextTextfield的最终结果将是[textFieldsArray[21] becomeFirstResponder]。显然它会跳过其他字段,当您单击下一步按钮时,您的最后一个字段将被设置为第一响应者。

于 2013-11-05T09:12:13.147 回答