J'ai un UIPickerView
que je suis sous-classe, ce qui fonctionne très bien. Cependant, j'ai une méthode que j'ai besoin d'appeler pour "réinitialiser" l'UIPicker à partir du UIViewController dans lequel il est vu. Cependant, il ne se déclenche jamais. Comment puis-je appeler func resetPicker()
à partir d'un autre contrôleur de vue?Comment appeler la méthode dans la sous-classe UIPickerView à partir d'un autre contrôleur de vue?
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
}
}
EDIT:
Ce qui suit ne fonctionne pas du viewController appelant le ScoreClockPicker
:
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) {
}
}
Ajoutez le code où vous créez objet de 'ScoreClockPicker'. Moyens ajouter le code de votre ViewController. –
@NiravD, voir modifier ci-dessus. J'ai essayé ça et ça ne marche pas. –
Où vous avez initialisé cet objet 'picker' nous montrer le code de cela aussi, Réinitialiser est l'action du bouton? –