Guest User

Untitled

a guest
Nov 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. //
  2. // SecondViewController.swift
  3. // NotificationCenter
  4. //
  5. // Created by strawb3rryx7 on 21.11.2017.
  6. // Copyright © 2017 strawb3rryx7. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class SecondViewController: UIViewController {
  12.  
  13. @IBOutlet weak var nameLabel: UILabel!
  14.  
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17.  
  18. // Diğer controllerdan sendNameNotificationKey notification kimliğini kullanarak gönderilen notificationları da burada receive edeceğiz. Bu yüzden observer tanımlıyoruz. Gönderilen verilere erişmek içinde selector'e onReceiveNotification adında bir method tanımladım ve onu da aşağıda oluşturdum.
  19. NotificationCenter.default.addObserver(self, selector: #selector(onReceivedNotification(notification:)), name: sendNameNotificationKey, object: nil)
  20. }
  21.  
  22. @objc func onReceivedNotification(notification: Notification) {
  23. // Notification'un userInfo'a sahip olup olmadığına bakıyoruz eğer değilse method devam etmiyor.
  24. guard let userInfo = notification.userInfo else {
  25. return
  26. }
  27.  
  28. // Eğer notification bir userInfo içeriyorsa zaten içerisinde "name" adında bir obje var mı onu kontrol ediyoruz var ise TextField içeriğini aşağıdaki gibi değiştiriyoruz.
  29. if let name = userInfo["name"] as? String {
  30. self.nameLabel.text = "İsmin: \(name)"
  31. }
  32. }
  33.  
  34. override func didReceiveMemoryWarning() {
  35. super.didReceiveMemoryWarning()
  36. // Dispose of any resources that can be recreated.
  37. }
  38.  
  39. }
Add Comment
Please, Sign In to add comment