$30 off During Our Annual Pro Sale. View Details »

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

417.72KI
August 31, 2018

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

417.72KI

August 31, 2018
Tweet

More Decks by 417.72KI

Other Decks in Technology

Transcript

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

    *OD d  w େखاۀͷެࣜΞϓϦडୗ։ൃ͕ϝΠϯ TFMGEFTDSJQUJPO
  2. -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
  3. -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͕ʜ
  4. -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ʜ
  5. 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
  6. 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ͰΫϥογϡճආ
  7. 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໰୊΋ղܾ
  8. .VMUJQMF%FTUJOBUJPO class SecondViewController: UIViewController { var message: Message = Message(value:

    "") } class ThirdViewController: UIViewController { var message: Message = Message(value: "") }
  9. .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 } }
  10. .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͕૿͑Δ౓ʹܕ νΣοΫͷίʔυ͕૿͑Δʜ
  11. .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: "") }
  12. 1SPUPDPM0SJFOUFE4FHVF override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if

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

    let messageView = segue.destination as? MessageView { messageView.message = message } } 1SPUPDPMͰUZQFTBGFDBTU
  14. 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 } } }
  15. 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 ͕͋ͬͨΒ۷ΓԼ͛ΔΑ
  16. 'JO