Jab
Jab

Reputation: 929

Subclass UIPickerView swift

I'm trying to create a subclass of UIPickerView so I can use my picker in multiple views. I'm trying to call my picker programmatically as inputView of a UITextField but I can't figure out how to initialise it correctly. I'm in doubt if this is the right approach and how I can get it to work. I hope any of you can help me.

UIPickerView subclass:

import UIKit

 class GroupPicker : UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource{

    var cdm = CoreDataManager()
    var groupObjList : [Group]!

    init() {
       groupObjList = cdm.groupList()
    }

    //MARK: - Delegates and data sources

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return groupObjList.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return groupObjList[row].title
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        println("picked \(groupObjList[row].title)")

    }        
}

How I try to call it in the view controller:

override func viewDidLoad() {
    super.viewDidLoad()

    groupPicker = GroupViewPicker()  //instead of UIPickerView()
    groupField.inputView = groupPicker
}

Upvotes: 2

Views: 3656

Answers (3)

Jab
Jab

Reputation: 929

It was easy like this.

GroupPicker : UIPickerView {

        override init(frame: CGRect){
            super.init(frame: frame)
            self.groupObjList = cdm.groupList()
        } 
}

Call it:

viewDidLoad() {
    groupField.inputView = GroupPicker(frame: CGRectZero)
}

Upvotes: 1

arthankamal
arthankamal

Reputation: 6413

You have to override your init method

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override init(frame: CGRect) {
    super.init(frame:frame)
    self.delegate = self
    self.dataSource = self
}

Upvotes: 2

Chris
Chris

Reputation: 8020

I presume you will want to present this modally?

If so, you could do the following:

override func viewDidLoad() {
    super.viewDidLoad()

    groupPicker = GroupViewPicker()  //instead of UIPickerView()
    //Send the data you want your picker view to handle.
    groupPicker.groupObjList = dataForPickerView
    self.presentViewController(viewControllerToPresent: groupPicker, animated: , completion: nil)
} 

And it should pop up and present itself modally

Tip:

This var groupObjPicker: [Group]! will blow up if you never pass in a group to this array (i.e. you dont set the property as shown above). A safer (depending on what you are trying to achieve ofc.) would be to declare it as an empty array like such var groupObjList = [Group](). Then you know that if nothing is passed to it, it will at least just return 0 when .count is called on it etc.

Upvotes: 0

Related Questions