Я пытаюсь сделать протокол и делегат в Swift, но у меня есть некоторые проблемы. Я хочу иметь кнопку переключателя в ячейках таблицы. Вот мой протокол:Протокол и делегирование в Swift
import Foundation
import UIKit
protocol CellProtocol {
func onSwitchToogle (sender : AnyObject , onCell : UITableViewCell)
}
Вот мой мобильный класс:
import UIKit
class Cell: UITableViewCell {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var flag: UISwitch!
var cellDelegate:CellProtocol!
@IBAction func Toogle(sender: AnyObject) {
if((cellDelegate?.onSwitchToogle(sender, onCell: self)) != nil){
}
}
}
А вот мой ViewController:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CellProtocol {
func onSwitchToogle(sender: AnyObject, onCell: UITableViewCell) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! Cell
cell.label.text = "sadsad"
return cell
}
}
Проблема в том, что никогда не входит в если условие в IBAction моего переключателя, и он никогда не входит в метод на ViewController.
Плюс ... 'onSwitchToogle()' ничего не возвращает, поэтому я даже не уверен, что вы пытаетесь сделать с этим оператором 'if' ... – nhgrif
Не делайте ваш делегат неявно развернутым ! –