Using "Next" as a Return Key

Using Next as a Return Key

Make sure your text fields have their delegate set and implement the textFieldShouldReturn method. This is the method that is called when the user taps the return key (no matter what it looks like).

The method might look something like this:

func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.field1 {
self.field2.becomeFirstResponder()
}

return true
}

The actual logic in here might vary. There are numerous approaches, and I'd definitely advise against a massive if/else chain if you have lots of text fields, but the gist here is to determine what view is currently active in order to determine what view should become active. Once you've determined which view should become active, call that view's becomeFirstResponder method.


For some code cleanliness, you might consider a UITextField extension that looks something like this:

private var kAssociationKeyNextField: UInt8 = 0

extension UITextField {
var nextField: UITextField? {
get {
return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
}
set(newField) {
objc_setAssociatedObject(self, &kAssociationKeyNextField, newField, .OBJC_ASSOCIATION_RETAIN)
}
}
}

And then change our textFieldShouldReturn method to look like this:

func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.nextField?.becomeFirstResponder()
return true
}

Once you've done this, it should simply be a matter of setting each text field's new nextField property in viewDidLoad:

self.field1.nextField = self.field2
self.field2.nextField = self.field3
self.field3.nextField = self.field4
self.field4.nextField = self.field1

Although if we really wanted, we could prefix the property with @IBOutlet, and that would allow us to hook up our "nextField" property right in interface builder.

Change the extension to look like this:

private var kAssociationKeyNextField: UInt8 = 0

extension UITextField {
@IBOutlet var nextField: UITextField? {
get {
return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
}
set(newField) {
objc_setAssociatedObject(self, &kAssociationKeyNextField, newField, .OBJC_ASSOCIATION_RETAIN)
}
}
}

And now hook up the nextField property in interface builder:

Sample Image

(Set up your delegate while you're here too.)


And of course, if the nextField property returns nil, the keyboard just hides.

iPhone keyboard's return key will move cursor to next textfield

implement UITextFieldDelegate

yourFirstTextFeld.delegate = self;
yourSecondTextFeld.delegate=self;

implement this method

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if(theTextField==yourFirstTextFeld){
[yourSecondTextFeld becomeFirstResponder];
}
return YES;
}

How UITextField move to next TextField When Click on Return Key in Keyboard


func textFieldShouldReturn(_ textField: UITextField) -> Bool {

if textField == txtFldSponsorID {
txtFldFullName.becomeFirstResponder()
} else if textField == txtFldFullName {
txtFldEmail.becomeFirstResponder()
} else if textField == txtFldEmail {
txtFldMobile.becomeFirstResponder()
} else if textField == txtFldMobile {
txtFldAddress.becomeFirstResponder()
} else {
txtFldCity.resignFirstResponder()
}
return true
}

You can use this above UITextField Delegate method to jump to next UItextField.

Xamarin iOS - Navigate to next text field with return key

You could make use of these functions

http://iosapi.xamarin.com/?link=M%3aMonoTouch.UIKit.UIResponder.BecomeFirstResponder

BecomeFirstResponder()

and

http://iosapi.xamarin.com/?link=M%3aMonoTouch.UIKit.UIResponder.ResignFirstResponder

ResignFirstResponder()

According to the documentation you should add the textField Delegate method like this

public virtual bool ShouldReturn (UITextField textField)
{

if (txtUsername.tag == 1) {

UITextField txtPassword = (UITextField)this.view.ViewWithTag(2);

txtPassword.BecomeFirstResponder();
}
else {
txtUsername.ResignFirstResponder();
}
return true;
}

Return next key of a given dictionary key, python 3.6+

Looping while k := next(key_iter) doesn’t stop correctly. Iterating manually with iter is done either by catching StopIteration:

iterator = iter(some_iterable)

while True:
try:
value = next(iterator)
except StopIteration:
# no more items

or by passing a default value to next and letting it catch StopIteration for you, then checking for that default value (but you need to pick a default value that won’t appear in your iterable!):

iterator = iter(some_iterable)

while (value := next(iterator, None)) is not None:
# …

# no more items

but iterators are, themselves, iterable, so you can skip all that and use a plain ol’ for loop:

iterator = iter(some_iterable)

for value in iterator:
# …

# no more items

which translates into your example as:

def next_key(d, key):
key_iter = iter(d)

for k in key_iter:
if k == key:
return next(key_iter, None)

return None

Move to next NSTableview Row when return key is pressed after editing

I managed to solve my issue, probably not the best method, but works 100% for me.

I changed my tableView to View-Based. I then added an action for the TextFields. After the action is performed, I entered the following code:

 tableWages.selectRowIndexes(NSIndexSet(index: tableWages.selectedRow + 1) as IndexSet, byExtendingSelection: false)
tableWages.editColumn(colNr, row: tableWages.selectedRow, with: nil, select: true)

So after editing the value and the Return Key is pressed, it moves to the next row and stays in the same column, making the next value editable.

Switching between Text fields on pressing return key in Swift

Make sure your UITextField delegates are set and the tags are incremented properly. This can also be done through the Interface Builder.

Here's a link to an Obj-C post I found: How to navigate through textfields (Next / Done Buttons)

class ViewController: UIViewController,UITextFieldDelegate {
// Link each UITextField (Not necessary if delegate and tag are set in Interface Builder)
@IBOutlet weak var someTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
// Do the next two lines for each UITextField here or in the Interface Builder
someTextField.delegate = self
someTextField.tag = 0 //Increment accordingly
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Try to find next responder
if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
nextField.becomeFirstResponder()
} else {
// Not found, so remove keyboard.
textField.resignFirstResponder()
}
// Do not add a line break
return false
}
}

How to pop/remove the next item (any) in a key-value-pair table in Lua?

There is a primitive function next, you can call next(t,k), where k is a key of the table t, returns a next key in the table, in an arbitrary order, and the value associated with this key.

If k is nil, next(t,k) returns the first element if there is one. So you can iterate the table from calling next(t,nil) and end when the next key is nil.

This is an simple example to demonstrate the use of next:

local t = {a = "va", b = "vb", c = "vc"}

local k,v = next(t,nil)
print(k,v)
k,v = next(t,k)
print(k,v)
k,v = next(t,k)
print(k,v)
k,v = next(t,k)
print(k,v)

Output:

a       va
c vc
b vb
nil nil


Related Topics



Leave a reply



Submit