簡體   English   中英

在靜態UITableView上使用UITextfields的上一個和下一個鍵盤按鈕

[英]Previous and Next keyboard buttons using UITextfields on Static UITableView

我正在使用帶有靜態單元格的UITableView來顯示帶有多個UITextFields的表單(每個表單都具有從9000到9015的唯一順序標簽)。 當我選擇UITextField ,鍵盤上會顯示一個UIToolbar ,其中有2個按鈕,上一個和下一個。 只要在屏幕上繪制上一個或下一個UITextField ,按鈕就可以正常工作,否則我無法選擇它,因為viewWithTag找不到該字段。

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

編輯:我IQKeyboardManager使用IQKeyboardManager但它具有相同的錯誤。 如果看不見這些單元格,則不會檢測到它們,因此下一個或上一個箭頭將被禁用...

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()
        }
    }

}

有沒有辦法總是繪制靜態單元格,或者我應該以不同的方式實現它?

如果賣出不在屏幕上,它將被釋放。 不管您使用靜態還是動態單元格。 您目前可能找不到的視圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM