Guest User

Untitled

a guest
Jun 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. protocol HashTagPickerDelegate {
  2.  
  3. func hastagPicked(hashtag:String)
  4. }
  5.  
  6. class HashtagPicker : UIViewController
  7. {
  8.  
  9. var delegate: HashTagPickerDelegate?
  10.  
  11. }
  12.  
  13. delegate.hashtagPicked(pickedHashtag)
  14.  
  15. class ParentView:UIViewController, HashTagPickerDelegate
  16. {
  17.  
  18. }
  19.  
  20. func prepare(for segue: UIStoryboardSegue, sender: AnyObject?)
  21. {
  22. if segue.identifier == "HastagPickerSegue" // use your segue id here
  23. {
  24. let hashtagPicker = segue.destinationViewController as! HashtagPickerView
  25. hashtagPicker.delegate = self
  26. }
  27. }
  28.  
  29. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ParentViewController.someActionToBePerformed), name: "myNotification", object: nil)
  30.  
  31. func someActionToBePerformed () {
  32. // this will be called when hashTag is changed
  33. // do something when hashTag is changed
  34. }
  35.  
  36. NSNotificationCenter.defaultCenter().postNotificationName("myNotification", object: nil)
  37.  
  38. // note the ": class" part
  39. protocol HashTagPickerDelegate: class {
  40. func picked(hashtag: String)
  41. }
  42.  
  43. class HashtagPicker: UIViewController {
  44. // if HashTagPickerDelegate wouldn't be limited to class,
  45. // we couldn't have made a weak reference here!
  46. weak var delegate: HashTagPickerDelegate?
  47.  
  48. // at some point, you call the delegate, it can be anywhere, this is just an example
  49. @IBAction func tappedHashtag(_ sender: Any) {
  50. delegate?.picked(hashtag: "bla")
  51. }
  52. }
  53.  
  54. class ParentView: UIViewController, HashTagPickerDelegate {
  55. func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  56. // we are presenting the nested controller
  57. if segue.identifier == "SegueHastagPickerContainer",
  58. let destinationController = segue.destination as? HashtagPicker {
  59. destinationController.delegate = self
  60. }
  61. }
  62. }
Add Comment
Please, Sign In to add comment