简体   繁体   中英

How to call method in UIPickerView subclass from another view controller?

I have a UIPickerView that I am subclassing, which works great. However, I have a method that I need to call to "reset" the UIPicker from the UIViewController in which it's being viewed. However, it never fires. How do I call func resetPicker() from another view controller?

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:

The following doesn't work from the viewController calling the 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) {
    }


}

From your code you haven't initialized the picker object that you are trying to use in button action. So simply initialized the picker before using it.

If you are making outlet of ScoreClockPicker then

@IBOutlet var picker: ScoreClockPicker!

OR you can initialized picker in viewDidLoad

self.picker = ScoreClockPicker()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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