如何调用UIPickerView inSwift的子类

时间:2015-05-04 17:08:18

标签: ios swift uipickerview

我有一个带有UIPickerView的ViewController作为单个控件myPickerView,它是一个MyPickerView类,我作为UIPickerView的子类创建。我通过myPickerView.viewDidLoad在ViewController viewDidLoad中调用myPickerView。但是,这不会执行MyPickerView的源函数。

我需要澄清我如何才能做到这一点。我的MyPickerView的原因是它有很多特殊的代码,我不想让主ViewController混乱。请参阅以下示例代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myPickerView: MyPickerView!
override func viewDidLoad() {
    super.viewDidLoad()
    myPickerView.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}


import UIKit
var gSep = ","
class MyPickerView: UIPickerView , UIPickerViewDataSource,     UIPickerViewDelegate {
var pickerData = [[" "],[" "]]
var category = [""]
var subCategory = [""]
var dictMenuList = [String:String]()

//MARK:- category/subcategory picker


func viewDidLoad() {
    println("MyPickerView: viewDidLoad")

    dictMenuList = ["Medical":"Sub-Cat 1.1,Sub-Cat 1.2,Sub-Cat 1.3,Sub-Cat 1.4,Sub-Cat 1.5,Sub-Cat 1.6,Sub-Cat 1.7",
        "Taxes": "Sub-Cat 2.1,Sub-Cat 2.2,Sub-Cat 2.3,Sub-Cat 2.4",
        "Bills": "Sub-Cat 3.1,Sub-Cat 3.2,Sub-Cat 3.3,Sub-Cat 3.4,Sub-Cat 3.5,Sub-Cat 3.6,Sub-Cat 3.7"]

    println("MyPickerView dictMenuList: \(dictMenuList)")



    self.reloadAllComponents()

    let firstKey = self.loadPickerWithCategory(0)
    self.loadPickerWithSubCategory(firstKey)

}


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    println("MyPickerView: numberOfComponentsInPickerView \(pickerData.count)")
    return pickerData.count
}

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

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if component == 0 {
        let selectedKey = category[row]
        loadPickerWithSubCategory(selectedKey)
    }
}

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

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
    var pickerLabel = UILabel()
    pickerLabel.textColor = UIColor.blackColor()
    pickerLabel.text = pickerData[component][row]
    pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 17)
    pickerLabel.textAlignment = NSTextAlignment.Center
    return pickerLabel
}

func loadPickerWithCategory (row: Int) -> String{
    println("loadPickerWithCategory")
    category = [String](dictMenuList.keys)
    println("MyPickerView: category: \(category)")
    if category.isEmpty {
        return ""
    }
    let n1 = dictMenuList.count
    pickerData[0].removeAll(keepCapacity: true)
    for i in 0 ..< n1
    {
        pickerData[0].append(category[i])
    }
    return category[row]
}

func loadPickerWithSubCategory (key: String) {
    println("MyPickerView: loadPickerWithSubCategory")
    let x = dictMenuList[key]
    subCategory = x!.componentsSeparatedByString(gSep)
    let n1 = subCategory.count
    pickerData[1].removeAll(keepCapacity: true)
    if subCategory.isEmpty {
        return
    }
    for i in 0 ..< n1
    {
        pickerData[1].append(subCategory[i])
    }

    self.reloadAllComponents()
}

}

2 个答案:

答案 0 :(得分:0)

首先,viewDidLoad()UIViewController类的一种方法,在控制器视图加载到内存后调用。阅读更多here。你不能在视图中使用它。

因此,您应该在自定义选取器类中实现init方法。我建议覆盖initWithFrameinitWithCoder并在那里设置您的组件。

您将初始化您的自定义选择器:

myPickerView = MyPickerView(frame: yourFrame)

答案 1 :(得分:0)

方法viewDidLoad是视图控制器方法,而不是视图方法。 UIPickerViewUIView的子类,而不是UIViewController,因此系统不会调用您的viewDidLoad方法。

您需要覆盖一个或多个init方法。

如果您正在从Storyboard或XIB加载选择器视图,则可能需要覆盖initWithCoder。

如果您在代码中创建选择器,则可能需要覆盖initWithFrame。

我有时会创建一个setup来自initWithCoder:initWithFrame:的方法awakeFromNib。这样就可以调用我的设置代码,无论视图对象是如何加载的。

我依稀记得在Swift中有一种更好的方法来处理这种决斗初始化问题,但我不记得它是什么。 (我还在学习斯威夫特。)

编辑:

我刚刚发现,您可以在加载视图并设置其所有插座后使用方法viewDidLoad进行设置。这大致相当于视图控制器的awakeFromNib调用。我应该早点想到这一点。

({{1}}是NSObject的一种方法,所以如果您不知道它存在,就很难找到。)