Skip to content

Instantly share code, notes, and snippets.

@BrandonShega
Created May 22, 2018 14:55
Show Gist options
  • Save BrandonShega/45c006b0bf8b3e82d25b57e93959ea19 to your computer and use it in GitHub Desktop.
Save BrandonShega/45c006b0bf8b3e82d25b57e93959ea19 to your computer and use it in GitHub Desktop.
Passing info to popup controller
//: A UIKit based Playground for presenting user interface
import UIKit
import Foundation
import PlaygroundSupport
struct Recipe {
let name: String
}
protocol MyDelegate: class {
func didAddToCart(recipe: Recipe)
}
class MyCell: UITableViewCell {
var recipe: Recipe?
weak var delegate: MyDelegate?
lazy var button: UIButton = {
let button = UIButton()
button.setTitle("Add To Cart", for: .normal)
button.setTitleColor(.green, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(addToCart), for: .touchUpInside)
return button
}()
func configure(with recipe: Recipe) {
self.recipe = recipe
textLabel?.text = recipe.name
contentView.addSubview(button)
setupConstraints()
}
func setupConstraints() {
NSLayoutConstraint.activate([
button.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -10),
button.widthAnchor.constraint(equalToConstant: 100),
button.heightAnchor.constraint(equalToConstant: 20)
])
}
@objc func addToCart() {
guard let recipe = recipe else { return }
delegate?.didAddToCart(recipe: recipe)
}
}
class MyPopupController: UIViewController {
var recipe: Recipe?
var recipeLabel: UILabel = {
let label = UILabel()
label.textColor = .black
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var closeButton: UIButton = {
let button = UIButton()
button.setTitleColor(.black, for: .normal)
button.setTitle("Close", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(close), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(recipeLabel)
view.addSubview(closeButton)
setupConstraints()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
recipeLabel.text = recipe?.name
}
func setupConstraints() {
NSLayoutConstraint.activate([
recipeLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
closeButton.topAnchor.constraint(equalTo: recipeLabel.bottomAnchor, constant: 100)
])
}
@objc func close() {
dismiss(animated: true, completion: nil)
}
}
class MyController: UIViewController {
var recipes: [Recipe] = []
lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.delegate = self
tableView.dataSource = self
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
recipes = [
Recipe(name: "Chocolate Chip Cookies"),
Recipe(name: "Banana Nut Pudding"),
Recipe(name: "Roasted Chicken"),
Recipe(name: "Barbecue Ribs")
]
tableView.register(MyCell.self, forCellReuseIdentifier: "MyCell")
view.addSubview(tableView)
setupConstraints()
}
func setupConstraints() {
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.leftAnchor.constraint(equalTo: view.leftAnchor),
tableView.rightAnchor.constraint(equalTo: view.rightAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
}
extension MyController: MyDelegate {
func didAddToCart(recipe: Recipe) {
let popup = MyPopupController()
popup.recipe = recipe
present(popup, animated: true, completion: nil)
}
}
extension MyController: UITableViewDelegate {}
extension MyController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recipes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as? MyCell else { return UITableViewCell() }
cell.configure(with: recipes[indexPath.row])
cell.delegate = self
return cell
}
}
PlaygroundPage.current.liveView = MyController()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment