-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
71 lines (51 loc) · 2.42 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// ViewController.swift
// ShoppingList
//
// Created by Julian Moorhouse on 03/08/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import UIKit
class ViewController: UITableViewController {
var shoppingList = [String]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Shopping List"
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(promptForItem))
let reset = UIBarButtonItem(barButtonSystemItem: .trash, target: self, action: #selector(resetList))
let share = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
navigationItem.leftBarButtonItems = [reset, share]
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shoppingList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Product", for: indexPath)
cell.textLabel?.text = shoppingList[indexPath.row]
return cell
}
@objc func resetList() {
shoppingList.removeAll(keepingCapacity: true)
tableView.reloadData()
}
@objc func promptForItem() {
let ac = UIAlertController(title: "Enter item", message: nil, preferredStyle: .alert)
ac.addTextField()
let submitAction = UIAlertAction(title: "Submit", style: .default) {
[weak self, weak ac] action in // specifies input into closure, ac is a weak reference
// using ac? as its a weak reference
guard let answer = ac?.textFields?[0].text?.lowercased() else { return }
self?.shoppingList.insert(answer, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
self?.tableView.insertRows(at: [indexPath], with: .automatic)
}
ac.addAction(submitAction)
present(ac, animated: true)
}
@objc func shareTapped() {
let list = shoppingList.joined(separator: "\n")
let vc = UIActivityViewController(activityItems: [list], applicationActivities: [])
vc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
present(vc, animated: true)
}
}