2015-11-01 16 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 = ... 
}