简体   繁体   English

在Swift中为UIPickerView委派方法

[英]Delegate Methods in Swift for UIPickerView

Just getting started in Swift and having trouble calling the delegate methods for a UIPickerView 刚开始使用Swift并且无法调用UIPickerView的委托方法

So far I have added the UIPickerViewDelegate to my class like so: 到目前为止,我已经将UIPickerViewDelegate添加到我的类中,如下所示:

class ExampleClass: UIViewController, UIPickerViewDelegate

I have also created my UIPickerView and set the delegate for it: 我还创建了我的UIPickerView并为其设置了委托:

@IBOutlet var year: UIPickerView
year.delegate = self

Now I am just having trouble turning the following into Swift code: 现在我无法将以下内容转换为Swift代码:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

Any help would be appreciated 任何帮助,将不胜感激

That's actually a method in the UIPickerViewDataSource protocol, so you'll want to make sure you set your picker view's dataSource property as well: year.dataSource = self . 这实际上是UIPickerViewDataSource协议中的一个方法,因此您需要确保设置选择器视图的dataSource属性: year.dataSource = self The Swift-native way seems to be to implement protocols in class extensions, like this: Swift本地方式似乎是在类扩展中实现协议,如下所示:

class ExampleClass: UIViewController {
    // properties and methods, etc.
}

extension ExampleClass: UIPickerViewDataSource {
    // two required methods

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

    func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
        return 5
    }
}

extension ExampleClass: UIPickerViewDelegate {
    // several optional methods:

    // func pickerView(pickerView: UIPickerView!, widthForComponent component: Int) -> CGFloat

    // func pickerView(pickerView: UIPickerView!, rowHeightForComponent component: Int) -> CGFloat

    // func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String!

    // func pickerView(pickerView: UIPickerView!, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString!

    // func pickerView(pickerView: UIPickerView!, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView!

    // func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
}

The delegate isn't responsible for calling that method. 委托人不负责调用该方法。 Instead it is called by the UIPickerView's data source. 相反,它由UIPickerView的数据源调用。 These are the two functions called by the UIPickerView data source that you need to implement: 这些是您需要实现的UIPickerView数据源调用的两个函数:

// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int

// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int

To ensure these functions get called, your class should also implement the data source protocol: 为确保调用这些函数,您的类还应实现数据源协议:

class ExampleClass: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource

And your picker's data source should be set: 并且应该设置您的选择器的数据源:

year.dataSource = self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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