2016-03-14 175 views
-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") 
    } 

} 

回答

0

你可以簡單地分配給像你的CustomPickerViewController一樣。您需要爲視圖控制器創建新的對象,然後分配委託,

對於如:

let customPickerVC = CustomPickerViewController() 
customPickerVC.delegate = self 
+0

由於這樣做,我已經加入'viewController.presentViewController'是現在工作 – SNos