简体   繁体   中英

Pass data from View Controller to View

I have main View Controller, where are located application's data. This data, I need to send to the view, which contains UIView and nested to him UITableView. That UITableView is responsible for show pop up menu with part of my data in cells (specifically title of the theme).

Interface of pop up menu

I have tried to do this by protocol and delegate, but it was not successful. Maybe I did something wrong or it in general impossible and I need to find another way out?

Data to pass

    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 for the View

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)

} }

There are several ways of achieving this but for this case the following seems simple and easy to achieve:

In your DropDownView class, declare listOfThemes array as following:

var listOfThemes: [Theme]!

Then in your mainVC when you initialize DropDownView, do the following before displaying the view:

fileprivate var dropDownView = DropDownView()
dropDownView.listOfThemes = themes // themes are the themes from your main VC
// now present it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM