简体   繁体   English

UIPickerView作为UITextView swift的inputView

[英]UIPickerView as inputView for UITextView swift

I have used UIPickerView as inputView for UITextView. 我已经将UIPickerView用作UITextView的inputView。 The selectRow: function doesn't select properly for alternate time selectRow:函数不能正确选择其他时间

Here is the code 这是代码

func textViewDidBeginEditing(_ textView: UITextView) {
    let inputView = UIView(frame: CGRect(x: 0,y: 0, width: self.view.frame.width, height: 240))
    let codePicker = UIPickerView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 240))
    codePicker.tag = 2

    if codePicker.dataSource == nil {
        codePicker.dataSource = self
    }
    if codePicker.delegate == nil {
        codePicker.delegate = self
    }

    codePicker.showsSelectionIndicator = true
    print("selectedBankRow: \(selectedRow)")
    codePicker.selectRow(selectedRow, inComponent: 0, animated: true)
    inputView.addSubview(codePicker)
    textView.inputView = inputView
    return
}

The print statement print correct row but the selectRow: function selects the previous to the previous value which was selected, what could be the error? 打印语句可打印正确的行,但selectRow:函数会选择上一个值而不是上一个选择的值,这可能是什么错误? Even the status bar goes black each alternate time only for this UITextView interaction. 即使是状态栏,也仅在此UITextView交互时每隔一次变黑。

Don't create UIPickerView every time 不要每次都创建UIPickerView

Create Global Object 创建全局对象

var codePicker = UIPickerView()

and in viewDidLoad Method 并在viewDidLoad方法中

add following code 添加以下代码

let inputView = UIView(frame: CGRect(x: 0,y: 0, width: self.view.frame.width, height: 240))
codePicker = UIPickerView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 240))
codePicker.tag = 2
codePicker.dataSource = self
codePicker.delegate = self

codePicker.showsSelectionIndicator = true
print("selectedBankRow: \(selectedRow)")
codePicker.selectRow(selectedRow, inComponent: 0, animated: true)
inputView.addSubview(codePicker)
textView.inputView = inputView

as you solved Status bar issue with 解决状态栏问题时

IQKeyboardManager.sharedManager().canAdjustAdditionalSafeAreaInsets = true

Hope it is helpful 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM