[英]Pass data from View Controller to View
我有主视图控制器,应用程序的数据在哪里。 这些数据,我需要发送到视图,其中包含 UIView 并嵌套到他的 UITableView。 UITableView 负责显示弹出菜单,其中包含我在单元格中的部分数据(特别是主题的标题)。
我试图通过协议和委托来做到这一点,但没有成功。 也许我做错了什么,或者大体上不可能,我需要找到另一种出路?
要传递的数据
fileprivate var themes = [
Theme(name: "Halloween",
emoji: ["🤡", "🎃", "👻", "☠️", "🦇", "🔮", "⚰️", "🍭", "👣", "🗡", "⚱️", "🕯", "🃏", "🔍", "💉", "👹", "👁", "😈"],
backgroundColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1),
buttonColor: #colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 1)),
Theme(name: "Sports",
emoji: ["🏓", "⚽️", "🏅", "🎳", "🥊", "🏊♂️", "🚵♀️", "⛳️", "🏈", "🥋", "🏏", "⛸", "🎣", "🏀", "🎱", "🏂", "🤽♂️", "🤸♂️"],
backgroundColor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1),
buttonColor: #colorLiteral(red: 0.9686274529, green: 0.78039217, blue: 0.3450980484, alpha: 1)),
Theme(name: "Flags",
emoji: ["🇺🇦", "🇺🇸", "🇧🇷", "🏳️🌈", "🇵🇱", "🇯🇵", "🇧🇪", "🇪🇸", "🇬🇧", "🇦🇺", "🇲🇾", "🇵🇼", "🇹🇷", "🇫🇮", "🇸🇪", "🏴", "🇩🇪", "🇪🇺"],
backgroundColor: #colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 1),
buttonColor: #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)),
Theme(name: "Animals",
emoji: ["🐶", "🦄", "🐣", "🐷", "🐒", "🐪", "🐋", "🐌", "🦊", "🐸", "🐼", "🐠", "🐿", "🐄", "🐭", "🐯", "🐔", "🐝"],
backgroundColor: #colorLiteral(red: 0.3098039329, green: 0.2039215714, blue: 0.03921568766, alpha: 1),
buttonColor: #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)),
Theme(name: "Food",
emoji: ["🍔", "🍟", "🍣", "🍿", "🍩", "🍦", "🍞", "🧀", "🥒", "🍏", "🥞", "🌭", "🍌", "🍫", "🍯", "🥘", "🍤", "🎂"],
backgroundColor: #colorLiteral(red: 0.1294117719, green: 0.2156862766, blue: 0.06666667014, alpha: 1),
buttonColor: #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)),
Theme(name: "Gadgets",
emoji: ["⌚️", "💻", "🖥", "🖱", "🖨", "📷", "📺", "📱", "💿", "📹", "📻", "🎙", "⏰", "📼", "📡", "⏲", "⌨️", "📠"],
backgroundColor: #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1),
buttonColor: #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1))
]
视图类
class DropDownView: UIView {
fileprivate var listOfThemes = ["1", "2", "3"] //test data
fileprivate struct Keys {
static let cellIdentifier = "cell"
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
} }
extension DropDownView: UITableViewDelegate, UITableViewDataSource {
fileprivate func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfThemes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Keys.cellIdentifier, for: indexPath)
cell.textLabel?.text = listOfThemes[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
} }
有几种方法可以实现这一点,但对于这种情况,以下似乎简单易行:
在DropDownView
类中,声明listOfThemes
数组如下:
var listOfThemes: [Theme]!
然后在您的 mainVC 中初始化 DropDownView 时,在显示视图之前执行以下操作:
fileprivate var dropDownView = DropDownView()
dropDownView.listOfThemes = themes // themes are the themes from your main VC
// now present it.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.