Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a <em>much</em> more elegant solution which blew me away the first time I saw it. Benefits:</p> <ul> <li>Closer to OSX textfield implementation where a textfield knows where the focus should go next</li> <li>Does not rely on setting or using tags -- which are, IMO fragile for this use case</li> <li>Can be extended to work with both <code>UITextField</code> and <code>UITextView</code> controls -- or any keyboard entry UI control</li> <li>Doesn't clutter your view controller with boilerplate UITextField delegate code</li> <li>Integrates nicely with IB and can be configured through the familiar option-drag-drop to connect outlets.</li> </ul> <p>Create a UITextField subclass which has an <code>IBOutlet</code> property called nextField. Here's the header:</p> <pre><code>@interface SOTextField : UITextField @property (weak, nonatomic) IBOutlet UITextField *nextField; @end </code></pre> <p>And here's the implementation:</p> <pre><code>@implementation SOTextField @end </code></pre> <p>In your view controller, you'll create the <code>-textFieldShouldReturn:</code> delegate method:</p> <pre><code>- (BOOL)textFieldShouldReturn:(UITextField *)textField { if ([textField isKindOfClass:[SOTextField class]]) { UITextField *nextField = [(SOTextField *)textField nextField]; if (nextField) { dispatch_async(dispatch_get_current_queue(), ^{ [nextField becomeFirstResponder]; }); } else { [textField resignFirstResponder]; } } return YES; } </code></pre> <p>In IB, change your UITextFields to use the <code>SOTextField</code> class. Next, also in IB, set the delegate for each of the 'SOTextFields'to 'File's Owner' (which is right where you put the code for the delegate method - textFieldShouldReturn). The beauty of this design is that now you can simply right-click on any textField and assign the nextField outlet to the next <code>SOTextField</code> object you want to be the next responder. </p> <p><img src="https://i.stack.imgur.com/XOfau.png" alt="Assigning nextField in IB"></p> <p>Moreover, you can do cool things like loop the textFields so that after the last one loses focus, the first one will receive focus again.</p> <p>This can easily be extended to automatically assign the <code>returnKeyType</code> of the <code>SOTextField</code> to a <code>UIReturnKeyNext</code> if there is a nextField assigned -- one less thing manually configure.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload