这是我的原始代码:
import UIKit
class CalculatorViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var firstButton: UIButton!
@IBOutlet weak var secondButton: UIButton!
@IBOutlet weak var thirdButton: UIButton!
@IBOutlet weak var fourthButton: UIButton!
@IBOutlet weak var fifthButton: UIButton!
@IBOutlet weak var nextPageButton: UIButton!
@IBOutlet weak var firstTextField: UITextField!
@IBOutlet weak var secondTextField: UITextField!
@IBOutlet weak var thirdTextField: UITextField!
@IBOutlet weak var fourthTextField: UITextField!
@IBOutlet weak var fifthTextField: UITextField!
@IBOutlet weak var pickerView: UIPickerView!
var firstButtonDataSource = ["1", "2", "3", "4"];
var secondButtonDataSource = ["White", "Red", "Green", "Blue"];
var thirdButtonDataSource = ["Mike", "Steve", "Ben", "Peter"];
var fourthButtonDataSource = ["Large", "Medium", "Small", "Extra-small"];
var fithButtonDataSource = ["USA", "UK", "France", "Germany"];
var lastPressedButton: UIButton?
override func viewDidLoad() {
super.viewDidLoad()
firstButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
secondButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
thirdButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
fourthButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
fifthButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
}
@objc func buttonClicked(sender:UIButton!) {
lastPressedButton = sender
if lastPressedButton == firstButton {
firstTextField.inputView = pickerView
} else if lastPressedButton == secondButton {
secondTextField.inputView = pickerView
} else if lastPressedButton == thirdButton {
thirdTextField.inputView = pickerView
} else if lastPressedButton == fourthButton {
fourthTextField.inputView = pickerView
} else if lastPressedButton == fifthButton {
fifthTextField.inputView = pickerView
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if lastPressedButton == firstButton {
return firstButtonDataSource.count;
} else if lastPressedButton == secondButton {
return secondButtonDataSource.count;
} else if lastPressedButton == thirdButton {
return thirdButtonDataSource.count;
} else if lastPressedButton == fourthButton {
return fourthButtonDataSource.count;
} else if lastPressedButton == fifthButton {
return fithButtonDataSource.count;
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if lastPressedButton == firstButton {
return firstButtonDataSource[row];
} else if lastPressedButton == secondButton {
return secondButtonDataSource[row];
} else if lastPressedButton == thirdButton {
return thirdButtonDataSource[row];
} else if lastPressedButton == fourthButton {
return fourthButtonDataSource[row];
} else if lastPressedButton == fifthButton {
return fithButtonDataSource[row];
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if lastPressedButton == firstButton {
self.firstTextField.text = firstButtonDataSource[row]
} else if lastPressedButton == secondButton {
self.secondTextField.text = secondButtonDataSource[row]
} else if lastPressedButton == thirdButton {
self.thirdTextField.text = thirdButtonDataSource[row]
} else if lastPressedButton == fourthButton {
self.fourthTextField.text = fourthButtonDataSource[row]
} else if lastPressedButton == fifthButton {
self.fifthTextField.text = fithButtonDataSource[row]
}
}
}
然后我被告知这样做:
” 看起来您从未将正确的文本字段设置为第一响应者。
顺便说一下,所有你的if / else if / else if ...构造都会更干净,因为你可以设置字典,按钮作为键,然后是包含数据源和选择器的结构查看该按钮:“ struct ButtonData {
let buttonDataSource: [String]
let buttonTextField: UITextField
}
let buttonsDict = [
firstButton: ButtonData(buttonDataSource: ["1", "2", "3", "4"],
buttonTextField: firstTextField),
secondButton: ButtonData(buttonDataSource: ["White", "Red", "Green", "Blue"],
buttonTextField: secondTextField),
thirdButton: ButtonData(buttonDataSource: ["Mike", "Steve", "Ben", "Peter"],
buttonTextField: thirdTextField),
fourthButton: ButtonData(buttonDataSource: ["Large", "Medium", "Small", "Extra-small"],
buttonTextField: fourthTextField),
fifthButton: ButtonData(buttonDataSource: ["USA", "UK", "France", "Germany"],
buttonTextField: fifthTextField)
]
和他说:“然后像你的按钮动作一样的代码可以使用字典:”
@objc func buttonClicked(sender:UIButton!) {
lastPressedButton = sender
guard let buttonStruct = buttonsDict[lastPressedButton] else { return }
let textField = buttonsStruct.buttonTextField
textField.inputView = pickerView
//I think this the code you need to show the picker
textField.becomeFirstResponder()
}
所以我的问题是如何将这两组新代码添加到我的原始代码并更新它?我尝试了很多方法和地点,但我在代码方面没有足够的经验,不能在我的项目中弄乱一些东西
答案 0 :(得分:-1)
此图片会有所帮助。此Red mark
适用于UIPickerViewDelegate
。
创建名为Swift
的空ButtonData
文件并复制此代码。
在ViewController
中,您可以尝试这样做。
@IBOutlet weak var firstButton: UIButton!
@IBOutlet weak var secondButton: UIButton!
@IBOutlet weak var thirdButton: UIButton!
@IBOutlet weak var fourthButton: UIButton!
@IBOutlet weak var fifthButton: UIButton!
@IBOutlet weak var firstTextField: UITextField!
@IBOutlet weak var secondTextField: UITextField!
@IBOutlet weak var thirdTextField: UITextField!
@IBOutlet weak var fourthTextField: UITextField!
@IBOutlet weak var fifthTextField: UITextField!
var buttonsDict = [UIButton : ButtonData]()
var lastPressedButton: UIButton?
@IBOutlet weak var pickerView: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
buttonsDict = [
firstButton: ButtonData(buttonDataSource: ["1", "2", "3", "4"],
buttonTextField: firstTextField),
secondButton: ButtonData(buttonDataSource: ["White", "Red", "Green", "Blue"],
buttonTextField: secondTextField),
thirdButton: ButtonData(buttonDataSource: ["Mike", "Steve", "Ben", "Peter"],
buttonTextField: thirdTextField),
fourthButton: ButtonData(buttonDataSource: ["Large", "Medium", "Small", "Extra-small"],
buttonTextField: fourthTextField),
fifthButton: ButtonData(buttonDataSource: ["USA", "UK", "France", "Germany"],
buttonTextField: fifthTextField)
]
firstButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
secondButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
thirdButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
fourthButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
fifthButton.addTarget(self, action:#selector(buttonClicked(sender:)), for: .touchUpInside)
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
// Do any additional setup after loading the view.
}
@objc func buttonClicked(sender:UIButton!) {
lastPressedButton = sender
guard let buttonStruct = buttonsDict[lastPressedButton!] else { return }
let textField = buttonStruct.buttonTextField
textField.inputView = pickerView
//I think this the code you need to show the picker
textField.becomeFirstResponder()
}
// STRUCT
import Foundation
import UIKit
struct ButtonData {
let buttonDataSource: [String]
let buttonTextField: UITextField
}