我一直在学习 iOS 开发并遇到了一个问题。
我有一个具有 UIPickerView
的类,如下所示(它一开始是隐藏的)
public class MyController: UIViewController,
UIPickerViewDelegate,
UITableViewDataSource,
UITableViewDelegate {
@IBOutlet var myPickerView: UIPickerView!
}
我有另一个类,我需要访问这个选择器 View 并在用户开始编辑文本字段(标签 = 2)时使其可见
public class MyClass2: UITableViewCell,
UITextFieldDelegate {
@IBOutlet vat myTextField: UITextField! {didSet {myTextField.delegate = self } }
public func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if (textField.tag == 2) {
// Somehow access the picker view from the other class and change its property, hidden, to false
}
}
}
谢谢。
最佳答案
您正在为 MyClass2 中的 activeViewController 创建实例,
static var activeViewController: UIViewController
当你在你的 MyController 上工作时,设置 activeViewController
MyClass2.activeViewController = self
最后,您将使用 activeViewController 更改 MyController 类中的任何内容,
if let myController = activeViewController as? MyController {
//Do something with your PickerView
myController.myPickerView = ...
}
关于ios - 如何在自定义类中访问 UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33467558/