下面的代码具有两个不同的文本字段ana和sampleTextfield。现在,sampleTextfield可以完美地调用arrray firstHit。但是我试图弄清楚如何进行模拟调用并显示数组secondHit。现在,sampleTextfield被标记为1,而ana被标记为2。我认为标记是实现此目的的最佳方法。
import UIKit
class ViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {
let myBUtton = UIButton()
@IBOutlet var ana : UITextField!
@IBOutlet var sampleTextField: UITextField!
var initialc:[NSLayoutConstraint] = []
var buttonCons:[NSLayoutConstraint] = []
var textCons:[NSLayoutConstraint] = []
var textCons2:[NSLayoutConstraint] = []
var textAna:[NSLayoutConstraint] = []
var selecteDay : String?
let firstHit = ["","X","9","8","7","6","5","4","3","2","1","0"]
let secondHit = ["\\","9","8","7","6","5","4","3","2","1","0"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createToolBar()
createDayPicker()
sampleTextField.placeholder = "Enter text here"
sampleTextField.font = UIFont.systemFont(ofSize: 15)
sampleTextField.translatesAutoresizingMaskIntoConstraints = false
sampleTextField.borderStyle = UITextBorderStyle.roundedRect
sampleTextField.autocorrectionType = UITextAutocorrectionType.no
sampleTextField.keyboardType = UIKeyboardType.default
sampleTextField.returnKeyType = UIReturnKeyType.done
sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
self.view.addSubview(sampleTextField)
ana.placeholder = "Enter text here"
ana.font = UIFont.systemFont(ofSize: 15)
ana.translatesAutoresizingMaskIntoConstraints = false
ana.borderStyle = UITextBorderStyle.roundedRect
ana.autocorrectionType = UITextAutocorrectionType.no
ana.keyboardType = UIKeyboardType.default
ana.returnKeyType = UIReturnKeyType.done
ana.clearButtonMode = UITextFieldViewMode.whileEditing;
ana.contentVerticalAlignment = UIControlContentVerticalAlignment.center
self.view.addSubview(ana)
sampleTextField.translatesAutoresizingMaskIntoConstraints = false
let leadingc2 = sampleTextField.widthAnchor.constraint(equalToConstant: 80)
let trailingC2 = sampleTextField.heightAnchor.constraint(equalToConstant: 40)
let topc2 = sampleTextField.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: -70)
let bottomc2 = sampleTextField.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -250)
textCons = [leadingc2,trailingC2,topc2,bottomc2]
let leadingc2a = ana.widthAnchor.constraint(equalToConstant: 80)
let trailingC2a = ana.heightAnchor.constraint(equalToConstant: 40)
let topc2a = ana.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 70)
let bottomc2a = ana.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -250)
textAna = [leadingc2a,trailingC2a,topc2a,bottomc2a]
ana.backgroundColor = .gray
NSLayoutConstraint.activate(textCons)
NSLayoutConstraint.activate(textAna)
}
func createDayPicker () {
let dayPicker = UIPickerView()
dayPicker.tag = 0
dayPicker.delegate = self
dayPicker.backgroundColor = .black
sampleTextField.inputView = dayPicker
}
func createToolBar() {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButonnt = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.cancle))
toolbar.setItems([doneButonnt], animated: false)
toolbar.isUserInteractionEnabled = true
sampleTextField.inputAccessoryView = toolbar
}
@objc func cancle() {
view.endEditing(true)
if sampleTextField.text == "X"{
ana.isHidden = true
sampleTextField.backgroundColor = .red
NSLayoutConstraint.deactivate(textCons)
NSLayoutConstraint.deactivate(textAna)
sampleTextField.translatesAutoresizingMaskIntoConstraints = false
let leadingc2 = sampleTextField.widthAnchor.constraint(equalToConstant: 80)
let trailingC2 = sampleTextField.heightAnchor.constraint(equalToConstant: 40)
let topc2 = sampleTextField.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: -70)
let bottomc2 = sampleTextField.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -250)
let rightCona = sampleTextField.rightAnchor.constraint(equalTo: self.view.rightAnchor)
let leftCona = sampleTextField.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
textCons2 = [leadingc2,trailingC2,topc2,bottomc2,rightCona,leftCona]
NSLayoutConstraint.activate(textCons2)
}
else {
NSLayoutConstraint.deactivate(textCons2)
ana.isHidden = false
sampleTextField.translatesAutoresizingMaskIntoConstraints = false
let leadingc2 = sampleTextField.widthAnchor.constraint(equalToConstant: 80)
let trailingC2 = sampleTextField.heightAnchor.constraint(equalToConstant: 40)
let topc2 = sampleTextField.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: -70)
let bottomc2 = sampleTextField.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -250)
textCons = [leadingc2,trailingC2,topc2,bottomc2]
let leadingc2a = ana.widthAnchor.constraint(equalToConstant: 80)
let trailingC2a = ana.heightAnchor.constraint(equalToConstant: 40)
let topc2a = ana.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 70)
let bottomc2a = ana.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -250)
textAna = [leadingc2a,trailingC2a,topc2a,bottomc2a]
ana.backgroundColor = .gray
NSLayoutConstraint.activate(textCons)
NSLayoutConstraint.activate(textAna)
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return firstHit.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return firstHit[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selecteDay = firstHit[row]
sampleTextField.text = selecteDay
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var lable: UILabel
if let view = view as? UILabel {
lable = view
} else {
lable = UILabel()
}
lable.textColor = .white
lable.textAlignment = .center
lable.font = UIFont(name: "Menlo-Regular", size: 17)
lable.text = firstHit[row]
return lable
}
}
答案 0 :(得分:1)
当您分配inputView时,给pickerView一个标签
let dayPicker = UIPickerView()
dayPicker.tag = 0 // it's default also , and set 1 for the other picker
//
使用标记区分
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return ( pickerView.tag == 0 ) ? firstHit.count : secondHit.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return ( pickerView.tag == 0 ) ? firstHit[row] : secondHit[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
selecteDay = firstHit[row]
sampleTextField.text = selecteDay
}
else {
selecteSec = secondHit[row]
anaTextField.text = selecteSec
}
}
答案 1 :(得分:0)
尝试
class ViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource,UITextFieldDelegate {
let firstHit = ["a","b"]
let secondHit = ["c","d","p"]
override func viewDidLoad() {
super.viewDidLoad()
...
sampleTextField.placeholder = "Enter text here"
sampleTextField.delegate = self
...
ana.placeholder = "Enter text here"
ana.delegate = self
...
}
let dayPicker = UIPickerView()
func createDayPicker () {
dayPicker.delegate = self
dayPicker.backgroundColor = .black
sampleTextField.inputView = dayPicker
ana.inputView = dayPicker
}
@objc func cancle() {
view.endEditing(true)
...
}
func createToolBar() {
...
sampleTextField.inputAccessoryView = toolbar
ana.inputAccessoryView = toolbar
}
var activeTextField = UITextField()
func textFieldDidBeginEditing(_ textField: UITextField) {
activeTextField = textField
dayPicker.reloadAllComponents()
}
var getArray: [String] {
if activeTextField == sampleTextField {
return firstHit
} else {
return secondHit
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return getArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return getArray[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if activeTextField == sampleTextField {
sampleTextField.text = getArray[row]
//
}
else {
//
ana.text = getArray[row]
}
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let lable = UILabel()
lable.text = getArray[row]
return lable
}
}
答案 2 :(得分:0)
对我来说,最好创建2个单独的选取器。
从架构角度来讲,我最好执行以下操作:
类似的东西:
class SomeController: UIViewController {
lazy var picker1: UIPickerView = {
return UIPickerView()
}()
lazy var picker2: UIPickerView = {
return UIPickerView()
}()
let arr1: [String] = []
let arr2: [String] = []
var p1delegate: PickerDelegate?
var p1dataSourse: PickerDataSource?
var p2delegate: PickerDelegate?
var p2dataSourse: PickerDataSource?
override func viewDidLoad() {
super.viewDidLoad()
configurePicker1()
configurePicker2()
}
func configurePicker1() {
p1delegate = PickerDelegate(array: arr1)
p1dataSourse = PickerDataSource(array: arr1)
picker1.delegate = p1delegate
picker1.dataSource = p1dataSourse
}
func configurePicker2() {
p2delegate = PickerDelegate(array: arr2)
p2dataSourse = PickerDataSource(array: arr2)
picker2.delegate = p2delegate
picker2.dataSource = p2dataSourse
}
}
class PickerDataSource: NSObject {
let array: [String]
init(array: [String]) {
self.array = array
}
}
extension PickerDataSource: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array.count
}
// ...
}
class PickerDelegate: NSObject {
let array: [String]
init(array: [String]) {
self.array = array
}
}
extension PickerDelegate: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array[row]
}
// ...
}
答案 3 :(得分:0)
只需将标签提供给两个选择器视图,然后声明2个变量即可:-
let dayPicker = UIPickerView()
dayPicker.tag = 0
let otherPicker = UIPickerView()
otherPicker.tag = 1
var firstHitPickerSelectedValue = ""
var secondHitPickerSelectedValue = ""
//then write pickerView delegate/dataSourcemethod as below:-
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return ( pickerView.tag == 0 ) ? firstHit.count : secondHit.count
// same as if (pickerView.tag == 0) {return firstHit.count} else {return secondHit.count}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return ( pickerView.tag == 0 ) ? firstHit[row] : secondHit[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
firstHitPickerSelectedValue = firstHit[row]
sampleTextField.text = firstHitPickerSelectedValue
}
else {
secondHitPickerSelectedValue = secondHit[row]
anyTextField.text = secondHitPickerSelectedValue
}
}
答案 4 :(得分:0)
我能想到的一种方法如下:
import UIKit
//Indicator to indicate which field is currently active at the moment
enum ActiveField {
case ana
case sample
}
//protocol that different source will be using or conforms to
protocol PickerData {
var source: [String] { get }
var active: ActiveField { get }
}
//The first source
struct FirstHit: PickerData {
var source: [String]
var active: ActiveField
}
//The second source
struct SecondHit: PickerData {
var source: [String]
var active: ActiveField
}
class ViewController: UIViewController, UITextFieldDelegate {
let myBUtton = UIButton()
@IBOutlet var ana : UITextField!
@IBOutlet var sampleTextField: UITextField!
//These are the actual data received or manually set as the case here
let firstHit = FirstHit(source: ["","X","9","8","7","6","5","4","3","2","1","0"], active: .ana)
let secondHit = SecondHit(source: ["\\","9","8","7","6","5","4","3","2","1","0"], active: .sample)
//This one will be used to determine the title displayed in picker view
var pickerDataSource: PickerData!
//We will use this pickerview for all inputs view
var dayPicker: UIPickerView!
// MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
//1. create the pickerView
createDayPicker()
//2. create the toolbar
createToolBar()
//3. set the pickerview as input to textFields (same pickerview)
ana.inputView = dayPicker
sampleTextField.inputView = dayPicker
//4. set the textField delegate so that when they become active we know what source to use
ana.delegate = self
sampleTextField.delegate = self
}
func createDayPicker () {
dayPicker = UIPickerView()
dayPicker.delegate = self
dayPicker.dataSource = self
dayPicker.backgroundColor = .black
}
func createToolBar() {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButonnt = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.cancle))
toolbar.setItems([doneButonnt], animated: false)
toolbar.isUserInteractionEnabled = true
sampleTextField.inputAccessoryView = toolbar
ana.inputAccessoryView = toolbar
}
@objc func cancle() {
view.endEditing(true)
//...Do stuff
}
// MARK: - TextField Delegate
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
//set the appropriate source
if textField == ana {
pickerDataSource = firstHit
} else if textField == sampleTextField {
pickerDataSource = secondHit
}
//reload picker
dayPicker.reloadAllComponents()
return true
}
}
只需将pickerView委托和dataSource分开进行扩展
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1 //since we only have one component to display
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.source.count //the count of element inside source
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerDataSource.source[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//to know which field is active so that we can identify and set value to that field we are using ActivePicker
switch pickerDataSource.active {
case .ana:
ana.text = pickerDataSource.source[row]
case .sample:
sampleTextField.text = pickerDataSource.source[row]
}
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var lable: UILabel
if let view = view as? UILabel {
lable = view
} else {
lable = UILabel()
}
lable.textColor = .white
lable.textAlignment = .center
lable.font = UIFont(name: "Menlo-Regular", size: 17)
lable.text = pickerDataSource.source[row]
return lable
}
}