[英]Declare self in another class in Swift
public class CustomSelectFields: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let cities = ["city1", "city2"]
let selectiveTextField = UITextField()
let pickerView = UIPickerView()
//normally it will get array of cities and frame position
public func CreateCustomSelectField() -> UITextField {
selectiveTextField.frame = CGRect(x: ScreenSize.width * 0.1, y: ScreenSize.height * 0.4, width: ScreenSize.width * 0.8, height: ScreenSize.height * 0.1)
selectiveTextField.placeholder = "Placehoder"
selectiveTextField.inputView = pickerView
//should i declare delegates and datasoruce here?
pickerView.delegate = self
pickerView.dataSource = self
return selectiveTextField
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return cities.count
}
public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return cities[row]
}
public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectiveTextField.text = cities[row]
selectiveTextField.resignFirstResponder()
}
}
I'm trying to create a custom UIPickerView and i want it to use in another classes. 我试图创建一个自定义的UIPickerView,我希望它可以在另一个类中使用。 When I declare it's delegate and datasource in the same class, data are not showing up.
当我在同一类中声明它的委托和数据源时,没有显示数据。
Also I try to declare in the class which is i use for viewController Like this: 我也尝试在用于viewController的类中声明如下:
let textField = customElements.CreateCustomSelectField()
customElements.pickerView.delegate = ??
customElements.pickerView.dataSource = ??
self.view.addSubview(textField)
How can I accomplish this delegate & datasource issue? 如何解决此委托和数据源问题?
Your design and your code are garbled. 您的设计和代码是乱码。 You've defined a class,
CustomSelectFields
, that looks like you want it to manage a text field and a picker view. 您已经定义了一个类
CustomSelectFields
,它看起来像您希望它来管理文本字段和选择器视图。 Then you have a method, CreateCustomSelectField
, that sounds like a convenience initializer for your CustomSelectFields
class. 然后,您有一个方法
CreateCustomSelectField
,它听起来像CustomSelectFields
类的便捷初始化程序。 However, it's actually an instance method that configures and returns a UITextField
that belongs to the CustomSelectFields
class. 但是,它实际上是配置并返回属于
CustomSelectFields
类的UITextField
的实例方法。
Your CustomSelectFields
class conforms to the UIPickerViewDelegate
and UIPickerViewDataSource
protocols, which suggests that you intend it to act as the delegate for the picker view. 您的
CustomSelectFields
类符合UIPickerViewDelegate
和UIPickerViewDataSource
协议,这建议您打算让它充当选择器视图的委托。 If that's the case, the CustomSelectFields
class should have an initializer that creates the picker view and text field and configures them initially. 在这种情况下,
CustomSelectFields
类应该具有一个初始化程序,该初始化程序创建选择器视图和文本字段并进行初始配置。
Something like this: 像这样:
public class CustomSelectFields: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let cities = ["city1", "city2"]
let selectiveTextField: UITextField
let pickerView: UIPickerView
init() {
selectiveTextField = UITextField()
selectiveTextField.frame = CGRect(x: ScreenSize.width * 0.1,
y: ScreenSize.height * 0.4,
width: ScreenSize.width * 0.8,
height: ScreenSize.height * 0.1)
selectiveTextField.placeholder = "Placehoder" //Hodor! HODOR!
selectiveTextField.inputView = pickerView
pickerView.delegate = self
pickerView.dataSource = self
pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
}
}
But even still, having a class that creates and configures view objects - but doesn't display or manage them - is kind of odd. 但即使如此,拥有一个创建和配置视图对象(但不显示或管理它们)的类也很奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.