This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SecondVC: UIViewController { | |
@IBOutlet weak var secondVCLabel: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(doThisWhenNotify), | |
name: NSNotification.Name(rawValue: myNotificationKey), | |
object: nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func tabToNotifyBack(_ sender: UIButton) { | |
NotificationCenter.default.post(name: Notification.Name(rawValue: myNotificationKey), object: self) | |
secondVCLabel.text = "Notification Completed!😜" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class FirstVC: UIViewController { | |
@IBOutlet weak var FirstVCLabel: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(doSomethingAfterNotified), | |
name: NSNotification.Name(rawValue: myNotificationKey), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: myNotificationKey), | |
object: nil, | |
queue: nil, | |
using:catchNotification) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func catchNotification(notification:Notification) -> Void { | |
guard let name = notification.userInfo!["name"] else { return } | |
FirstVCLabel.text = "My name, \(name) has been passed! 😄" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidDisappear(_ animated: Bool) { | |
super.viewDidDisappear(true) | |
NotificationCenter.default.removeObserver(self) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddTaskViewController: UIViewController { | |
@IBOutlet weak var taskTextField: UITextField! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
@IBAction func buttonTapped(_ sender: UIButton) { | |
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext | |
let task = Task(context: context) // Link Task & Context | |
task.name = taskTextField.text! | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func saveContext () { | |
let context = persistentContainer.viewContext | |
if context.hasChanges { | |
do { | |
try context.save() | |
} catch { | |
let nserror = error as NSError | |
fatalError("Unresolved error \(nserror), \(nserror.userInfo)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
@IBOutlet weak var tableView: UITableView! | |
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext | |
var tasks: [Task] = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView.delegate = self | |
tableView.dataSource = self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = UITableViewCell() | |
let task = tasks[indexPath.row] | |
if let myName = task.name { | |
cell.textLabel?.text = myName | |
} | |
return cell | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
OlderNewer