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

标签 ios keyboard uitextfield uitextview uitoolbar

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

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

-(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;
    }

}}

现在,当我的任何文本字段被点击时,我对文本字段和 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,其他是文本字段。请帮助我理解为什么我的所有字段都被跳过,而我的最后一个字段被设置为下一个按钮点击时的第一响应者

最佳答案

关于ios - keytoolbar 上的下一个按钮将最后一个字段设置为第一响应者,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19784945/

相关文章:

ios - TabviewController如何显示未读消息(如角标(Badge))?

keyboard - XCode 6.1 iOS 8键盘十进制垫不正确

ios - 如何在iOS8中显示键盘

swift - 清除文本域初始值

ios - UITextField 清除故障 - 文本突出显示,选择插入符出现

ios - Objective c iphone - 如何区分生产构建与 qa/debug testng 构建

ios - 使用 Alamofire 数据填充表格单元格

linux - 有没有添加未列出字符的方法?

command-line - Ubuntu服务器命令行上无法识别的字符

ios - 从 Swift 3.2 迁移到 Swift 4 出现 "failed to import bridging header "错误