简体   繁体   中英

Swift 2 - protocol delegate not called

I am trying to implement a delegate in order to call events in main viewController.

When I try to call the delegate in the main view controller I get the error:

Instance member 'delegate' cannot be used on type 'CustomPickerViewController'

What am I missing here?

IN my delegate file I have:

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)
        }
    }


}

Main View Controller:

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")
    }

}

You can simply assign like you did with the CustomPickerViewController . You need to create new object for that view controller and then assign the delegate,

For eg :

let customPickerVC = CustomPickerViewController()
customPickerVC.delegate = self 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM