簡體   English   中英

將數據從視圖控制器傳遞到視圖

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM