Swift : 从一个文本框移动到另一个文本框
Swift : Move from one textfield to another
有一种情况,当用户在文本字段中输入 两个字符时,焦点应移动到下一个文本字段。
下面是我用过的代码:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
let newStr = (text as NSString)
.stringByReplacingCharactersInRange(range, withString: string)
if newStr.isEmpty {
return true
}
let intvalue = Int(newStr)
if textField.tag == 101 { print("101") // only 2 integer
return (intvalue >= 0 && intvalue <= 99) ? true : false
}
else if textField.tag == 102 { print("102") // only 4 integer
return (intvalue >= 0 && intvalue <= 9999) ? true : false
}
}
return true
}
如何实现?
此外,数字小键盘上没有完成按钮吗?
您应该在用户输入 2 个字符后告诉下一个文本字段成为第一响应者,如下所示:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if (textField.text?.characters.count == 2) {
textField.resignFirstResponder()
yourNextTextField.becomeFirstResponder()
}
return true
}
Next
和 Done
按钮的可能性是在数字键盘上方使用 UIToolbar
。这样,您将为这两个按钮分配一个功能,因为它们是 UIBarButtonItem
。
我在Objective C
里有一些示例代码,如果你需要,我可以把它转换成Swift
。
要创建 UIToolbar
,请使用:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nextIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(nextTouched:)];
// the middle button is to make the Done button align to right
[toolBar setItems:[NSArray arrayWithObjects:nextButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
picker1.inputAccessoryView = toolBar;
picker2.inputAccessoryView = toolBar;
picker3.inputAccessoryView = toolBar;
要实现 Next
功能,请使用这种代码:
-(void) nextTouched:(UIBarButtonItem*)itemClicked {
if [([picker1 isFirstResponder])
[picker2 becomeFirstResponder];
else if [([picker2 isFirstResponder])
picker3 becomeFirstResponder];
}
// This code in ViewDidLoad
txtCardDetails3.addTarget(self, action: #selector(VerifyCardViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
func textFieldDidChange(textField: UITextField){
let text = textField.text
if textField.tag == 101 { // Set tag at design time
if text?.utf16.count==2 {
txtCardDetails4.becomeFirstResponder()
}
}
}
// This code as it is
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
let newStr = (text as NSString)
.stringByReplacingCharactersInRange(range, withString: string)
if newStr.isEmpty {
return true
}
let intvalue = Int(newStr)
if textField.tag == 101 { print("101")
return (intvalue >= 0 && intvalue <= 99) ? true : false
}
else if textField.tag == 102 { print("102")
return (intvalue >= 0 && intvalue <= 9999) ? true : false
}
}
return true
}
有一种情况,当用户在文本字段中输入 两个字符时,焦点应移动到下一个文本字段。
下面是我用过的代码:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
let newStr = (text as NSString)
.stringByReplacingCharactersInRange(range, withString: string)
if newStr.isEmpty {
return true
}
let intvalue = Int(newStr)
if textField.tag == 101 { print("101") // only 2 integer
return (intvalue >= 0 && intvalue <= 99) ? true : false
}
else if textField.tag == 102 { print("102") // only 4 integer
return (intvalue >= 0 && intvalue <= 9999) ? true : false
}
}
return true
}
如何实现?
此外,数字小键盘上没有完成按钮吗?
您应该在用户输入 2 个字符后告诉下一个文本字段成为第一响应者,如下所示:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if (textField.text?.characters.count == 2) {
textField.resignFirstResponder()
yourNextTextField.becomeFirstResponder()
}
return true
}
Next
和 Done
按钮的可能性是在数字键盘上方使用 UIToolbar
。这样,您将为这两个按钮分配一个功能,因为它们是 UIBarButtonItem
。
我在Objective C
里有一些示例代码,如果你需要,我可以把它转换成Swift
。
要创建 UIToolbar
,请使用:
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nextIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(nextTouched:)];
// the middle button is to make the Done button align to right
[toolBar setItems:[NSArray arrayWithObjects:nextButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
picker1.inputAccessoryView = toolBar;
picker2.inputAccessoryView = toolBar;
picker3.inputAccessoryView = toolBar;
要实现 Next
功能,请使用这种代码:
-(void) nextTouched:(UIBarButtonItem*)itemClicked {
if [([picker1 isFirstResponder])
[picker2 becomeFirstResponder];
else if [([picker2 isFirstResponder])
picker3 becomeFirstResponder];
}
// This code in ViewDidLoad
txtCardDetails3.addTarget(self, action: #selector(VerifyCardViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
func textFieldDidChange(textField: UITextField){
let text = textField.text
if textField.tag == 101 { // Set tag at design time
if text?.utf16.count==2 {
txtCardDetails4.becomeFirstResponder()
}
}
}
// This code as it is
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if let text = textField.text {
let newStr = (text as NSString)
.stringByReplacingCharactersInRange(range, withString: string)
if newStr.isEmpty {
return true
}
let intvalue = Int(newStr)
if textField.tag == 101 { print("101")
return (intvalue >= 0 && intvalue <= 99) ? true : false
}
else if textField.tag == 102 { print("102")
return (intvalue >= 0 && intvalue <= 9999) ? true : false
}
}
return true
}