Skip to content

Instantly share code, notes, and snippets.

View bobleesj's full-sized avatar
🚩
Back to work

Sangjoon Bob Lee bobleesj

🚩
Back to work
View GitHub Profile
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)
}
@IBAction func tabToNotifyBack(_ sender: UIButton) {
NotificationCenter.default.post(name: Notification.Name(rawValue: myNotificationKey), object: self)
secondVCLabel.text = "Notification Completed!😜"
}
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),
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: myNotificationKey),
object: nil,
queue: nil,
using:catchNotification)
func catchNotification(notification:Notification) -> Void {
guard let name = notification.userInfo!["name"] else { return }
FirstVCLabel.text = "My name, \(name) has been passed! 😄"
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(true)
NotificationCenter.default.removeObserver(self)
}
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!
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)")
}
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
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 {