我試圖弄清楚很長時間。有人能告訴我爲什麼我的委託方法從未被調用。它的tvOS項目,但我相信它應該作爲簡單的iOS應用程序。點擊按鈕我有一個彈出的表格視圖和選擇我想要更新按鈕標籤與選定的選項。代表返回nil tvOS
protocol PopupSelectionHandlerProtocol{
func UpdateSelected(data:String)
}
class PopupViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var myTable: UITableView!
let months = [1,2,3,4,5,6,7,8,9,10,11,12]
let days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
let yearsRange = [2015,2016,2017,2018,2019,2020]
var popupType:String!
var delegate:PopupSelectionHandlerProtocol?
override func viewDidLoad() {
super.viewDidLoad()
popupType = "months"
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (popupType == "months"){
return 12
}else if (popupType == "days"){
return 31
}else if (popupType == "years")
{
return 6
}
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = String(months[indexPath.row])
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text)
delegate?.UpdateSelected((tableView.cellForRowAtIndexPath(indexPath)?.textLabel?.text)!)
self.dismissViewControllerAnimated(true, completion: nil)
}
}
然後這 -
class VacationPlannerController: UIViewController,PopupSelectionHandlerProtocol {
@IBOutlet weak var fromMonth: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let popupDelegate = PopupViewController()
popupDelegate.delegate = self
}
func UpdateSelected(data:String){
print("Inside UpdateSelected VacationPlannerController \(data)")
fromMonth.titleLabel?.text = data
}
}
你有沒有按Ctrl +拖動或點擊右鍵暫停+拖動從的tableView到ViewController設置委託在故事板? –
是的,我已經做了 –
其中委託方法沒有得到一個調用,updateSelected或tableView的委託方法? –