2015-11-01 38 views
1

我一直在学习iOS开发并遇到问题。 我有具有UIPickerView如下:a类(它是隐藏的开始)如何访问自定义类中的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 
    } 
} 
} 

谢谢。

+0

你应该阅读协议和delegati上;然后在创建单元格时将视图控制器设置为委托,并根据需要让单元调用委托方法 – Paulw11

回答

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 = ... 
}