我有一个我的 UIPickerView
,我正在划分,效果很好。但是,我有一种需要调用的方法来"重置"它正在查看的UiviewController。但是,它永远不会开火。如何从另一个视图控制器调用func resetPicker()
?
import Foundation
import UIKit
class ScoreClockPicker: UIPickerView {
//PickerView
let timerMinutesArray = Array(00...20)
let timerSecondsArray = Array(00...59)
let periodArray = ["1st", "2nd", "3rd", "OT", "SO"]
//Picker return values
var numberOfRowsInComponentReturnValue = 0
var titleForRowReturnValue = ""
var widthForComponentReturnValue = ""
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
self.dataSource = self
}
func resetPicker() {
print("resetPicker")
self.selectRow(0, inComponent: 0, animated: true)
self.selectRow(0, inComponent: 1, animated: true)
self.selectRow(0, inComponent: 3, animated: true)
}
}
extension ScoreClockPicker: UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
numberOfRowsInComponentReturnValue = periodArray.count
} else if component == 1 {
numberOfRowsInComponentReturnValue = timerMinutesArray.count
} else if component == 2 {
numberOfRowsInComponentReturnValue = 1
} else if component == 3 {
numberOfRowsInComponentReturnValue = timerSecondsArray.count
}
return numberOfRowsInComponentReturnValue
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 4
}
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
var componentWidth = 0
if component == 0 {
componentWidth = 140
} else if component == 1 {
componentWidth = 40
} else if component == 2 {
componentWidth = 30
} else if component == 3 {
componentWidth = 40
}
return CGFloat(componentWidth)
}
}
extension ScoreClockPicker: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("didSelectRow")
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
titleForRowReturnValue = periodArray[row]
} else if component == 1 {
titleForRowReturnValue = String(describing: timerMinutesArray[row])
} else if component == 2 {
titleForRowReturnValue = ":"
} else if component == 3 {
titleForRowReturnValue = String(format: "%02d",timerSecondsArray[row])
}
return titleForRowReturnValue
}
}
编辑:
以下内容无法从访问ScoreClockPicker
的ViewController起作用:
import UIKit
class ScoreClockViewController: UIViewController {
@IBOutlet weak var resetButton: UIButton!
@IBOutlet weak var okButton: UIButton!
var picker: ScoreClockPicker?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//Hide the Navigation Bar
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
//@IBOutlet
@IBAction func reset(_ sender: Any) {
print("reset")
picker?.resetPicker() //Call the ScoreClockPicker
// picker?.selectRow(0, inComponent: 0, animated: true)
// picker?.selectRow(0, inComponent: 1, animated: true)
// picker?.selectRow(0, inComponent: 3, animated: true)
}
@IBAction func ok(_ sender: Any) {
}
}
从您的代码中,您尚未初始化要在按钮操作中使用的picker
对象。因此,在使用之前只需初始化picker
。
如果您要制作ScoreClockPicker
的出口,则
@IBOutlet var picker: ScoreClockPicker!
或您可以在viewDidLoad
中初始化picker
self.picker = ScoreClockPicker()