我想在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
不要讓你的委託隱式地解開! –