keytoolbar上的下一个按钮将最后一个字段设置为第一响应者,

时间:2013-11-05 08:53:19

标签: ios keyboard uitextfield uitextview uitoolbar

我在键盘工具栏上有两个按钮,下一个按钮总是将最后一个字段设置为我的第一个响应者,跳过所有中间的..加上这个键盘工具栏也没有显示在最后一个字段上。香港专业教育学院有一个存储所有textfield和textview对象的数组,并点击下一步和上一个我使用这个数组来确定哪个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;
    }

}}

现在,当我点击任何短信时,我对textfield和textview使用 didbeginediting 函数。 这是我的功能代码

  - (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,其他对象是文本字段。请帮助我理解为什么我的所有领域都被滑雪,我的最后一个领域被设置为下一次按钮点击的第一响应者

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)