Upgrade to Pro — share decks privately, control downloads, hide ads and more …

プロトコルでStoryboardともっと仲良くなる

7a78f23eef1b0e883ef44c229a54f0bb?s=47 417.72KI
August 31, 2018

 プロトコルでStoryboardともっと仲良くなる

7a78f23eef1b0e883ef44c229a54f0bb?s=128

417.72KI

August 31, 2018
Tweet

More Decks by 417.72KI

Other Decks in Technology

Transcript

  1. ϓϩτίϧͰ 4UPSZCPBSEͱ΋ͬͱ஥ྑ͘ͳΔ !J04%$  5XJUUFS2JJUB!@LJ (JU)VC,*

  2. w ΤϯδχΞྺ೥໨
 +BWB1)1,PUMJO4XJGUFUDʜ  w J04ྺ೥͘Β͍ 4UPSZCPBSEѪ޷ऀ  w J3JEHF

    *OD d  w େखاۀͷެࣜΞϓϦडୗ։ൃ͕ϝΠϯ TFMGEFTDSJQUJPO
  3. 5ZQJDBMTUPSZXJUI 4UPSZCPBSE w JEFOUJpFSͷUZQP w TFHVFͱ͔WDͱ͔DFMMͱ͔ʜ w QSFQBSF'PS4FHVFͷංେԽ w มߋʹऑ͍

  4. typo͸R.swiftͰ ͳΜͱ͔Ͱ͖Δ

  5. QSFQBSF'PS4FHVFͷ ංେԽ໰୊

  6. ݱঢ়ͷ4UPSZCPBSEͷੑ্࣭
 ׬શͳղܾ͸ແཧ

  7. 4UPSZCPBSE4FHVF ͸࢖͍ͮΒ͍࢖Θͳ͍

  8. None
  9. None
  10. 4UPSZCPBSEͷຊ࣭͸ ը໘ભҠΛ ίʔυ͔Β੾Γ཭͢͜ͱ

  11. ΋ͪΖΜ ׬શʹ੾Γ཭͢͜ͱ͸Ͱ͖ͳ͍ ओʹը໘ؒͰͷ஋ͷड͚౉͠

  12. ͦͷͨΊͷ 4UPSZCPBSE4FHVF

  13. 4UPSZCPBSEͱ஥ྑ͘ͳΔ 㲈 4FHVFͱ஥ྑ͘ͳΔ

  14. QSFQBSF'PS4FHVFͷ ංେԽ໰୊

  15. 1SPUPDPMΛ࢖͑͹ ܰݮͰ͖ΔͷͰ͸ʁ

  16. &YBNQMF

  17. 4JNQMF4UPSZCPBSE 'JSTU 7JFX$POUSPMMFS 4FDPOE 7JFX$POUSPMMFS .FTTBHF .FTTBHF

  18. -FHBDZ1BUUFSOPG QSFQBSF'PS4FHVF - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"second"])

    { SecondViewController *vc = segue.destinationViewController; vc.message = self.message; } } 0CKFDUJWF$ override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "second" { let second = segue.destination as! SecondViewController second.message = message } } 4XJGU
  19. -FHBDZ1BUUFSOPG QSFQBSF'PS4FHVF - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"second"])

    { SecondViewController *vc = segue.destinationViewController; vc.message = self.message; } } 0CKFDUJWF$ override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "second" { let second = segue.destination as! SecondViewController second.message = message } } 4XJGU ͜͜ͷUZQP͕ʜ
  20. -FHBDZ1BUUFSOPG QSFQBSF'PS4FHVF - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"second"])

    { SecondViewController *vc = segue.destinationViewController; vc.message = self.message; } } 0CKFDUJWF$ override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "second" { let second = segue.destination as! SecondViewController second.message = message } } 4XJGU 'PSDF$BTUJOHʜ
  21. 6TJOHEFTUJOBUJPO override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if

    let second = segue.destination as? SecondViewController { second.message = message } } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { switch segue.destination { case let second as SecondViewController: second.message = message default: break } } *GMFU TXJUDIMFU
  22. 6TJOHEFTUJOBUJPO override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if

    let second = segue.destination as? SecondViewController { second.message = message } } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { switch segue.destination { case let second as SecondViewController: second.message = message default: break } } *GMFU TXJUDIMFU UZQFTBGFDBTUͰΫϥογϡճආ
  23. 6TJOHEFTUJOBUJPO override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if

    let second = segue.destination as? SecondViewController { second.message = message } } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { switch segue.destination { case let second as SecondViewController: second.message = message default: break } } *GMFU TXJUDIMFU *EFOUJpFSͷUZQP໰୊΋ղܾ
  24. ίʔυͰݺͿඞཁ͕ແ͍ͳΒ 
 4UPSZCPBSEଆͰ΋*EFOUJpFS Λઃఆ͢Δඞཁ͕ແ͘ͳΔʂ

  25. .VMUJQMF%FTUJOBUJPO 'JSTU 7JFX$POUSPMMFS 4FDPOE 7JFX$POUSPMMFS 5IJSE 7JFX$POUSPMMFS .FTTBHF .FTTBHF .FTTBHF

  26. .VMUJQMF%FTUJOBUJPO class SecondViewController: UIViewController { var message: Message = Message(value:

    "") } class ThirdViewController: UIViewController { var message: Message = Message(value: "") }
  27. .VMUJQMF%FTUJOBUJPO override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if

    let second = segue.destination as? SecondViewController { second.message = message } if let third = segue.destination as? ThirdViewController { third.message = message } }
  28. .VMUJQMF%FTUJOBUJPO override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if

    let second = segue.destination as? SecondViewController { second.message = message } if let third = segue.destination as? ThirdViewController { third.message = message } } ಉ͡ϓϩύςΟΛ࣋ͭผͷ 7JFX$POUSPMMFS͕૿͑Δ౓ʹܕ νΣοΫͷίʔυ͕૿͑Δʜ
  29. ͦ΋ͦ΋

  30. 4UPSZCPBSEͷຊ࣭͸ ը໘ભҠΛ ίʔυ͔Β੾Γ཭͢͜ͱ

  31. ͳͷʹ

  32. QSFQBSF'PS4FHVFͷॲཧ͕ ˓˓7JFX$POUSPMMFSʹ ґଘ͍ͯ͠ΔͬͯͲ͏ͳͷΑ

  33. 4FHVFXFBLUPDIBOHF 7$ 4FHVF EFTU

  34. 4FHVFXFBLUPDIBOHF 7$ 4FHVF EFTU EFTUʹભҠ͢ΔͰ

  35. 4FHVFXFBLUPDIBOHF 7$ 4FHVF EFTU EFTUͷΫϥεʹ߹Θͤͯ ϓϩύςΟηοτ͢ΔͰ EFTUʹભҠ͢ΔͰ

  36. 4FHVFXFBLUPDIBOHF 7$ 4FHVF EFTU ϑΝοʂʁ ભҠઌมΘͬͨͰ EFTU

  37. 4FHVFTUSPOHUPDIBOHF 7$ 4FHVF EFTU QSPUPDPM

  38. 4FHVFTUSPOHUPDIBOHF 7$ 4FHVF EFTUʹભҠ͢ΔͰ EFTU QSPUPDPM

  39. 4FHVFTUSPOHUPDIBOHF 7$ 4FHVF QSPUPDPMʹ߹Θͤͯ ϓϩύςΟηοτ͢ΔͰ EFTUʹભҠ͢ΔͰ EFTU QSPUPDPM

  40. 4FHVFTUSPOHUPDIBOHF 7$ 4FHVF ಉ͡QSPUPDPM͔ͩΒ ॲཧม͑΁ΜͰ ભҠઌมΘͬͨͰ EFTU QSPUPDPM EFTU

  41. EFTUJOBUJPOΛ QSPUPDPMͰந৅Խ͢Ε͹ มߋʹڧ͘ͳΔ Т

  42. 1SPUPDPMJ[FQSPQFSUZ 'JSTU 7JFX$POUSPMMFS 4FDPOE 7JFX$POUSPMMFS 5IJSE 7JFX$POUSPMMFS .FTTBHF .FTTBHF .FTTBHF

  43. 1SPUPDPMJ[FQSPQFSUZ 'JSTU 7JFX$POUSPMMFS 4FDPOE 7JFX$POUSPMMFS 5IJSE 7JFX$POUSPMMFS .FTTBHF .FTTBHF .FTTBHF

    .FTTBHF 7JFX .FTTBHF
  44. .FTTBHF7JFX1SPUPDPM protocol MessageView: class { var message: Message { get

    set } } class SecondViewController: UIViewController, MessageView { var message: Message = Message(value: "") } class ThirdViewController: UIViewController, MessageView { var message: Message = Message(value: "") }
  45. 1SPUPDPM0SJFOUFE4FHVF override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if

    let messageView = segue.destination as? MessageView { messageView.message = message } }
  46. 1SPUPDPM0SJFOUFE4FHVF override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if

    let messageView = segue.destination as? MessageView { messageView.message = message } } 1SPUPDPMͰUZQFTBGFDBTU
  47. 1SPUPDPM0SJFOUFE4FHVF w ભҠઌͷΫϥεʹґଘ͠ͳ͍
 㲈มߋʹڧ͍QSFQBSF'PS4FHVF͕ॻ͚Δʂ w 1SPUPDPM୯ҐͰ؅ཧ͢Ε͹ྑ͍ͷͰංେԽ΋ܰݮʂ w *EFOUJpFSΛ࢖Θͳ͍ͷͰUZQP໰୊΋ղܾʂ ʁ

  48. EFTUJOBUJPOJOTJEF OBWJHBUJPOUBC#BS extension UIStoryboardSegue { var embeddedDestination: UIViewController { return

    detectDestination(destination) } } private extension UIStoryboardSegue { func detectDestination(_ vc: UIViewController) -> UIViewController { switch vc { case let nav as UINavigationController: guard let top = nav.topViewController else { return nav } return detectDestination(top) case let tab as UITabBarController: guard let sel = tab.selectedViewController else { return tab } return detectDestination(sel) default: return vc } } }
  49. EFTUJOBUJPOJOTJEF OBWJHBUJPOUBC#BS override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let messageView = segue.embeddedDestination as? MessageView { messageView.message = message } } 6*/BWJHBUJPO$POUSPMMFS 6*5BC#BS$POUSPMMFS ͕͋ͬͨΒ۷ΓԼ͛ΔΑ
  50. $PODMVTJPO w 4UPSZCPBSEͱ஥ྑ͘ͳΔʹ͸·ͣ4FHVF͔Β w 1SPUPDPMࢦ޲Ͱมߋʹڧ͍4FHVFΛ

  51. 4FHVFΛ੍͢ऀ͸ 4UPSZCPBSEΛ੍͢

  52. 'JO