我想在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
}
}
的问题是:它永远不会在if条件进入我的开关的IBAction,它永远不会进入ViewController的方法。
加上...'onSwitchToogle()'不会返回任何东西,所以我甚至不知道你要用这个'if'语句来做什么...... – nhgrif
不要让你的委托隐式地解开! –