gpt4 book ai didi

ios - 尝试设置 UIPickerView 的数据源时出错

转载 作者:行者123 更新时间:2023-11-28 12:52:27 26 4
gpt4 key购买 nike

我收到此错误:/Users/natumyers/Desktop/proj/SignUp2ViewController.swift:11:7: Type 'SignUp2ViewController' does not conform to protocol 'UIPickerViewDataSource'。我正在学习本教程 http://codewithchris.com/uipickerview-example/但我有一个独立于默认 View Controller 的 View Controller 。

import UIKit

class SignUp2ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var signuplabel: UITextField!

var labelText = String()

@IBOutlet weak var focusPicker: UIPickerView!
var focusPickerData: [String] = [String]()

override func viewDidLoad() {

super.viewDidLoad()

// Connect data:
self.focusPicker.delegate = self
self.focusPicker.dataSource = self

focusPickerData = ["type1","type2","type3","type4"]

signuplabel.text = labelText
}

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

解决方案

这些功能是实现这一目标的关键。我补充说:

// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}

// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return focusPickerData.count
}
// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return focusPickerData[row]
}

在 didReceiveMemoryWarning 函数之后。

最佳答案

您的 View Controller 不符合 UIPickerViewDataSource 协议(protocol)。您需要实现这两个必需的方法:

numberOfComponentsInPickerView(_:)
pickerView(_:numberOfRowsInComponent:)

The data source provides the picker view with the number of components, and the number of rows in each component, for displaying the picker view data. Both methods in this protocol are required.

https://developer.apple.com/library/prerelease/ios/documentation/iPhone/Reference/UIPickerViewDataSource_Protocol/index.html#//apple_ref/occ/intfm/UIPickerViewDataSource/numberOfComponentsInPickerView:

关于ios - 尝试设置 UIPickerView 的数据源时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36073032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com