如何在自定义类中访问UIPickerView

时间:2015-11-01 21:41:52

标签: ios swift uipickerview

我一直在学习iOS开发并遇到问题。 我有一个类UIPickerView如下(它隐藏起来)

public class MyController: UIViewController, 
                           UIPickerViewDelegate, 
                           UITableViewDataSource, 
                           UITableViewDelegate {

    @IBOutlet var myPickerView: UIPickerView!
}

我有另一个类,我需要访问此选择器视图,并在用户开始编辑文本字段(标签= 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
  }
 }
}

感谢。

1 个答案:

答案 0 :(得分:0)

您正在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 = ...
}