Reputation: 702
i've got two buttons on keyboard toolbar, the next button always sets the last field as my first responder, skipping all middle ones.. plus this keyboard toolbar is also not showing on that last field. ive got an array that store all the textfield and textview object and on click of next and prev i use this array to determine which obj will become first responder following is my code.
this is where i create a keyboard tool bar
-(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];}
this is where i create an array of objects
-(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];}
now here are my next and prev button code
-(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;
}
}}
now at time when my any textfiels is clicked i use didbeginediting function for both textfield and textview.. here are my func code
- (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];
}}
the very last object is textview, others are text fields. please help me understand why my all fields are being skiped and my last field is being set as first responder on next button click
Upvotes: 1
Views: 228
Reputation: 5268
My best advice for your requirement is BSKeyboardControls .
Please have a look
https://github.com/simonbs/BSKeyboardControls#installation
https://github.com/simonbs/BSKeyboardControls#usage
Upvotes: 1
Reputation: 891
Your implementation is wrong. Coz of the for loop, The final result of -(void)gotoNextTextfield would be that [textFieldsArray[21] becomeFirstResponder]. and obviouslyit wil skip other fields and your last field would be set as first responder when u click next button.
Upvotes: 1