在静态 UITableView 上使用 UITextfields 的上一个和下一个键盘按钮 [英] Previous and Next keyboard buttons using UITextfields on Static UITableView

查看:21
本文介绍了在静态 UITableView 上使用 UITextfields 的上一个和下一个键盘按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有静态单元格的 UITableView 来显示一个带有多个 UITextFields 的表单(每个都有一个从 9000 到 9015 的唯一有序标签).当我选择一个 UITextField 时,键盘会显示一个 UIToolbar,它有 2 个按钮,上一个和下一个.只要在屏幕上绘制上一个或下一个 UITextField,按钮就可以正常工作,否则我无法选择它,因为 viewWithTag 找不到该字段.

I am using a UITableView with static cells to show a form with several UITextFields (each with a unique ordered tag from 9000 to 9015). When i select a UITextField, the keyboard shows up with a UIToolbar that has 2 buttons, previous and next. The buttons work fine as long as the previous or next UITextField is drawn on screen, otherwise i can not select it because viewWithTag can't find the Field.

演示:http://s15.postimg.org/5xjsoiupl/ezgif_com_video_to_gif.gif

我尝试使用 IQKeyboardManager 但它有相同的错误.如果单元格不可见,则不会检测到它们,因此禁用下一个或上一个箭头...

I tryed using IQKeyboardManagerbut it has the same bug. If the cells are not visible then they are not detected so the next or previous arrow is disabled...

UITextFields

let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
numberToolbar.barStyle = UIBarStyle.Default
numberToolbar.tintColor = color_green

prev_keyboard_button = UIBarButtonItem(title: "Previous", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardPrevButtonTapped:")
next_keyboard_button = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardNextButtonTapped:")

numberToolbar.items = [
    prev_keyboard_button,
    next_keyboard_button,
    UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
    UIBarButtonItem(title: "OK", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardOKButtonTapped:")]

    numberToolbar.sizeToFit()
// bind vars
var_input_name.inputAccessoryView = numberToolbar
var_input_name.delegate = self
var_input_name.tag = 9000  // 9000 + index (0,1,2,3,..)
...

上一个和下一个代码:

func textFieldDidBeginEditing(textField: UITextField) {

    // current tag
    current_input_tag = textField.tag

    // check if it is the first
    prev_keyboard_button.enabled = !(textField.tag == 9000)

    // check if it is the last
    next_keyboard_button.enabled = !(textField.tag == 9015)

}

func keyboardPrevButtonTapped(sender: UIBarButtonItem) {

    if current_input_tag > 9000 {
        // find next input
        if let input = self.view.viewWithTag(current_input_tag - 1) as? UITextField {
            input.becomeFirstResponder()
        }
    }

}

func keyboardNextButtonTapped(sender: UIBarButtonItem) {

    if current_input_tag < 9015 {
        // find next input
        if let input = self.view.viewWithTag(current_input_tag + 1) as? UITextField {
            input.becomeFirstResponder()
        }
    }

}

有没有办法始终绘制静态单元格,还是应该以不同的方式实现?

Is there a way to always draw the static cells or should i be implementing this differently?

推荐答案

如果销售不在屏幕上,它将被释放.无论您使用静态单元格还是动态单元格.您尝试查找的视图可能此时不存在

if the sell is out of the screen it will be released. It's no matter if you use static or dynamic cells. Probably views you try to find is not exists at this moment

这篇关于在静态 UITableView 上使用 UITextfields 的上一个和下一个键盘按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆