[英]Passing Data between Classes Without Storyboards (Delegation)
我正在嘗試使用委托將數據從一個類發送到另一個類。
我在此類中設置了委托:
class AnswerViewController: UIViewController, tagToIndexDelegate{
override func viewDidLoad() {
super.viewDidLoad()
getDictionary()
}
func getDictionary() {
let myCell = MyCell()
myCell.delegate = self
print(123)
}
func finishPassing(dictionary: Dictionary<Int, Int>) {
print(dictionary)
}
}
並通過此類中的委托獲取數據:
protocol tagToIndexDelegate: class {
func finishPassing(dictionary: Dictionary<Int,Int>)
}
class MyCell: UITableViewCell, YSSegmentedControlDelegate{
var delegate: tagToIndexDelegate?
func segmentedControl(_ segmentedControl: YSSegmentedControl, willPressItemAt index: Int) {
tagToIndex[actionButton.tag] = index
delegate?.finishPassing(dictionary: tagToIndex)
}
}
我還無法獲取要在AnswerViewController
類中打印的dictionary
值。 AnswerViewController
絕對可以正常工作,因為每當我按下按鈕以選擇到該視圖控制器時,都會執行打印語句(直到按下按鈕時才會創建AnswerViewController
的實例)。
值得注意的是,我沒有在該項目中使用情節提要,否則我可以結合使用委托和情節提要segue來完成任務。
這里不對勁:
func segmentedControl(_ segmentedControl: YSSegmentedControl, willPressItemAt index: Int) {
tagToIndex[actionButton.tag] = index
delegate?.finishPassing(dictionary: tagToIndex)
}
您正在更新tagToIndex[actionButton.tag] = index,
但是沒有回tagToIndex[actionButton.tag] = index,
。
希望這會有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.