1

I have a relatively large iPhone application with many views. I would like implement a next/previous option on my keyboard. I have managed to implement it UI-wise, with some code examples i saw online, but all of them are assuming we need to add code to each view controller to implement the actual transition between the text fields.

My question is: is there a general way to know, given some text field, who is the next field in order? (i.e without refactoring each of my view controllers)

I ask this question because when i use the iPhone simulator and press the computer's Tab key - the switch between the fields happen, so i wonder if there is a built-in or generic way to implement it on iOS.

clarification: is there a way of doing it without adding a specific code for each type of view controller? (adding a generic code is acceptable)

Eliktz
  • 572
  • 1
  • 4
  • 27
  • No. There is no way to do it without adding code logic to navigate between fields. – Geek Jun 30 '13 at 13:32
  • You should have put that as an answer, not a comment. Comments are for clarification only. – SevenBits Jun 30 '13 at 13:36
  • i'll clarify: is there a way of doing it without adding a specific code for each type of view controller? (adding a generic code is fine) – Eliktz Jun 30 '13 at 13:44
  • 1
    This has some solutions, and one that does not require tagging the `UItextfields` http://stackoverflow.com/questions/1347779/how-to-navigate-through-textfields-next-done-buttons – JSA986 Jun 30 '13 at 14:36

3 Answers3

2

I want to write how i solved this problem, with the help of many good answers given to me here :)

First, i could not create fully generic code that creates tab regardless of the view it is in. Instead i created this thing, which i think is the most generic solution with the firstResponder method not working:

  • i created custom toolbar with my next/previous/done buttons and appropriate actions delegate.
  • than i extended UIViewController by adding category "Tab". this category declares a fieldsArray and implements the delegate method.
  • Now what every specific view controller needs to do (beside importing the category) is to provide this fieldsArray according to its properties and calling the init method which adds the buttons toolbar to this fields

I hope you could benefit from this, and again thanks for all the good answers

Eliktz
  • 572
  • 1
  • 4
  • 27
0

you could have a method in a utility class that takes as arguments a textfield and a viewcontroller. then you could use the "tag"-attribute of the textfields to find the next textfield in that viewcotroller, assuming that you assigned the tags accordingly. numbers would be great, i think. a simple callback method in the vc could handle the focus-change. thats about as generic as i can see right now.

katzenhut
  • 1,742
  • 18
  • 26
  • thanks for your answer. but using the tag property is one of the things i was trying to avoid, since most of my tables are being built on the fly, and putting all to tag order is a bit messy process. – Eliktz Jun 30 '13 at 14:10
  • @Eliktz - the tags are just a suggestion. you can determine the next field any way you like, the mechanism stays the same. (for example you could use x-y-coordinates to find the next field?) – katzenhut Jun 30 '13 at 15:15
0

This is some generic code that I came up with:

// add a property for the fieldsArray

//add this in viewDidLoad
_fieldsArray = [[NSMutableArray alloc] init];
NSArray *viewsArray = [self.view subviews];
for (id view in viewsArray) {
    if ([view isKindOfClass:NSClassFromString(@"UITextField")]) {
        [_fieldsArray addObject:view];
    }
}

//add this in your action that switches the fields
for (UITextField *field in _fieldsArray) {
    if ([field isFirstResponder]) {
        if ([fieldsArray lastObject] == field) {
            [_fieldsArray[0] becomeFirstResponder];
        }else {
            NSUInteger nextIndex = [_fieldsArray indexOfObject:field] + 1;
            [_fieldsArray[nextIndex] becomeFirstResponder];
        }
        break;
    }
}

Before using it it should be improved.

1) find all subviews of self.view recursively

2) do some checks if the arrays are empty or nil or have just one object in them.

Good luck!

Nikola Kirev
  • 3,152
  • 3
  • 22
  • 30