Review, Review and More Review
//
// ViewController.swift
// BirthdayTracker
//
import UIKit
protocol AddBirthdayViewControllerDelegate {
func addBirthdayViewController(_ addBirthdayViewController: AddBirthdayViewController, didAddBirthday birthday: Birthday)
}
class AddBirthdayViewController: UIViewController {
@IBOutlet var firstNameTextField: UITextField!
@IBOutlet var lastNameTextField: UITextField!
@IBOutlet var birthdatePicker: UIDatePicker!
var delegate: AddBirthdayViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
birthdatePicker.maximumDate = Date()
}
@IBAction func saveTapped(_ sender: UIBarButtonItem) {
print("The save button was tapped.")
let firstName = firstNameTextField.text ?? ""
let lastName = lastNameTextField.text ?? ""
print("My name is \(firstName) \(lastName).")
let birthdate = birthdatePicker.date
let newBirthday = Birthday(firstName: firstName, lastName: lastName, birthdate: birthdate)
print("Created a Birthday!")
print("First name: \(newBirthday.firstName)")
print("Last name: \(newBirthday.lastName)")
print("Birthdate: \(newBirthday.birthdate)")
delegate?.addBirthdayViewController(self, didAddBirthday: newBirthday)
dismiss(animated: true, completion: nil)
}
@IBAction func cancelTapped(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
}
========
//
// BirthdaysTableViewController.swift
// BirthdayTracker
//
// Created by Hao Tran on 3/17/18.
// Copyright © 2018 iOS Kids. All rights reserved.
//
import UIKit
class BirthdaysTableViewController: UITableViewController, AddBirthdayViewControllerDelegate {
var birthdays = [Birthday]()
let dateFormatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .none
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return birthdays.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "birthdayCellIdentifier", for: indexPath)
let birthday = birthdays[indexPath.row]
cell.textLabel?.text = birthday.firstName + " " + birthday.lastName
cell.detailTextLabel?.text = dateFormatter.string(from: birthday.birthdate)
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
// MARK: - AddBirthdayViewControllerDelegate
func addBirthdayViewController(_ addBirthdayViewController: AddBirthdayViewController, didAddBirthday birthday: Birthday) {
birthdays.append(birthday)
tableView.reloadData()
}
}