-1
我想實現一個delegate
爲了調用主viewController中的事件。Swift 2 - 協議委託不叫
當我打電話的main view controller
委託我得到的錯誤:
Instance member 'delegate' cannot be used on type 'CustomPickerViewController'
缺少什麼我在這裏?
在我的委託文件我有:
protocol CustomPickerViewControllerDelegate: class {
func CustomPickerDidSelectRow(sender:CustomPickerViewController)
}
//MARK: The Main Class
public class CustomPickerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
// DELEGATE
weak var delegate: CustomPickerViewControllerDelegate?
override public func viewDidLoad() {
super.viewDidLoad()
customPicker.delegate = self
customPicker.dataSource = self
.....
}
public func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
}else {
customLabel.text = arrayCustom[row]
self.delegate?.CustomPickerDidSelectRow(self)
}
}
}
主視圖控制器:
class ViewController: UIViewController, CustomPickerViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
CustomPickerViewController.delegate = self // ERROR
}
func CustomPickerDidSelectRow(sender: CustomPickerViewController) {
print("ok")
}
}
由於這樣做,我已經加入'viewController.presentViewController'是現在工作 – SNos