Advanced Segue 2019年のSegue事情

0d04162c7a3c2907c059a06dbcd23cf4?s=47 tokorom
September 05, 2019

Advanced Segue 2019年のSegue事情

iOSDC Japan 2019 day0 2019/09/05 18:40〜 Track B

0d04162c7a3c2907c059a06dbcd23cf4?s=128

tokorom

September 05, 2019
Tweet

Transcript

  1. Advanced Segue 2019೥ͷSegueࣄ৘ ॴ ༑ଠ / Yuta Tokoro, iOS &

    tvOS App Programmer #iOSDC Japan 2019 day0 2019/09/05 18:40ʙ Track B
  2. ॴ ༑ଠ / Yuta Tokoro @tokorom tokorom Spinners Inc.

  3. ࣭໰ • 1. ͝୲౰ͷΞϓϦͰStoryboardΛ࢖͍ͬͯ·͔͢ʁ • 2. ͝୲౰ͷΞϓϦͰSegueΛ࢖͍ͬͯ·͔͢ʁ

  4. Agenda • Basic Segue • iOS 13 New Features! •

    Advanced Tips •
  5. Agenda ✓Basic Segue • iOS 13 New Features! • Advanced

    Tips •
  6. Segue ͱ͸ʁ

  7. Segueͱ͸ʁ • Segueͱ͸UIStoryboardSegueͷ͜ͱ • Segue͸StoryboardΛ࢖ͬͨ։ൃ࣌ʹͷΈར༻Մೳ • Segue͸ը໘ભҠͰར༻͢Δ •

  8. Segueͱ͸ʁ • Segueͱ͸UIStoryboardSegueͷ͜ͱ • Segue͸StoryboardΛ࢖ͬͨ։ൃ࣌ʹͷΈར༻Մೳ • Segue͸ը໘ભҠͰར༻͢Δ •

  9. Segueͷछྨ • Standard SegueʢViewControllerΛ։͘ʣ • Embed SegueʢViewControllerͷೖΕࢠʣ • Unwind SegueʢಛఆͷViewController·ͰḪΔʣ

    • Custom SegueʢͳΜͰ΋ΞϦʣ •
  10. Standard Segue

  11. Embed Segue

  12. Unwind Segue ViewController ViewController ViewController ViewController ViewController

  13. Custom Segue class AwsomeSegue: UIStoryboardSegue { override func perform() {

    // ͳΜͰ΋ΞϦ } }
  14. SegueΛ࢖͏Pros • StoryboardͰ׬݁Ͱ͖Δ෦෼͕૿͑Δ • prepareForSegueͳͲͰҰݩ؅ཧͰ͖Δ • Apple Way͔Β֎Εͳ͍ •

  15. Static TableViewͱͷ૬ੑൈ܈

  16. Static TableViewͱͷ૬ੑൈ܈

  17. prepareForSegue

  18. Segueͷछྨ • Standard SegueʢViewControllerΛ։͘ʣ • Embed SegueʢViewControllerͷೖΕࢠʣ • Unwind SegueʢಛఆͷViewController·ͰḪΔʣ

    • Custom SegueʢͳΜͰ΋ΞϦʣ •
  19. prepareForSegue override func prepare(for segue: …) { if let acceptable

    = segue.destination as? FooAcceptable { if let foo = sender as? Foo { acceptable.accept(foo: foo) } } }
  20. viewDidAppear͕ݺ͹Εͳ͍ύλʔϯ

  21. viewDidAppear͕ݺ͹Εͳ͍ύλʔϯ override func prepare(for segue: …) { segue.destination.presentationController?.delegate = self

    } func presentationControllerDidDismiss( _ presentationController: UIPresentationController) { // present͞ΕͨViewController͕ด͡ΒΕͨͷΛݕ஌ }
  22. SegueΛ࢖͏Cons • ViewControllerͷinitializerΛࣗ෼Ͱݺ΂ͳ͍ • prepareForSegue͕ෳࡶʹͳΓ͕ͪ •

  23. Agenda ✓Basic Segue ✓iOS 13 New Features! • Advanced Tips

  24. SegueΛ࢖͏Cons • ViewControllerͷinitializerΛࣗ෼Ͱݺ΂ͳ͍ • prepareForSegue͕ෳࡶʹͳΓ͕ͪ •

  25. ViewControllerʹ஋Λ౉͢ ViewController ViewController Foo

  26. override func prepare(for segue: …) { if let acceptable =

    segue.destination as? FooAcceptable { if let foo = sender as? Foo { acceptable.accept(foo: foo) } } } SegueΛ࢖͏έʔεʢ͜Ε·Ͱʣ
  27. override func prepare(for segue: …) { if let acceptable =

    segue.destination as? FooAcceptable { if let foo = sender as? Foo { acceptable.accept(foo: foo) } } } class FooViewController: UIViewController, FooAcceptable { var foo: Foo! func accept(foo: Foo) { self.foo = foo } } SegueΛ࢖͏έʔεʢ͜Ε·Ͱʣ
  28. class FooViewController: UIViewController { let foo: Foo } ͜͏͍ͨ͠

  29. Segue Action

  30. Segue Action Source Destination

  31. Segue Action class SourceViewController: UIViewController { @IBSegueAction func sample1(_ coder:

    NSCoder) -> UIViewController? { return DestinationViewController(coder: coder, value: 1) } @IBSegueAction func sample2(_ coder: NSCoder) -> UIViewController? { return DestinationViewController(coder: coder, value: 2) } }
  32. Segue Action class SourceViewController: UIViewController { @IBSegueAction func sample1(_ coder:

    NSCoder) -> UIViewController? { return DestinationViewController(coder: coder, value: 1) } @IBSegueAction func sample2(_ coder: NSCoder) -> UIViewController? { return DestinationViewController(coder: coder, value: 2) } }
  33. Segue Action

  34. Segue Action

  35. Segue Action class FooViewController: UIViewController { let value: Int init?(coder:

    NSCoder, value: Int) { self.value = value super.init(coder: coder) } required init?(coder: NSCoder) { fatalError() } override func viewDidLoad() { // label.textʹvalueΛ୅ೖ } } ☺
  36. SegueΛ࢖͏Cons • ViewControllerͷinitializerΛࣗ෼Ͱݺ΂ͳ͍ • prepareForSegue͕ෳࡶʹͳΓ͕ͪ •

  37. SegueΛ࢖͏Cons • ViewControllerͷinitializerΛࣗ෼Ͱݺ΂ͳ͍ • prepareForSegue͕ෳࡶʹͳΓ͕ͪ •

  38. Segue ࢖ͬͯ΋͍͍ͷͰ͸ʁ

  39. Embed Segue + Segue Action

  40. Agenda ✓Basic Segue ✓iOS 13 New Features! ✓Advanced Tips •

  41. Unwind Segueͷੈք

  42. UnwindͰ͖Δઌͷ৚݅ @IBAction func unwindToList(segue: UIStoryboardSegue) { }

  43. Ͳ͔͜ΒͰ΋Unwind ViewController ViewController ViewController ViewController ViewController

  44. ϊϯίʔυͰUnwind

  45. Unwind Segue͸มߋʹڧ͍ ViewController ViewController ViewController ViewController ViewController

  46. Unwind Segue͸มߋʹڧ͍ ViewController ViewController ViewController Remove ViewController New

  47. ڞ௨ը໘͔ΒͷUnwind ViewController ڞ௨ ViewController ViewController ViewController

  48. Unwind Segueͷͦͷଞͷ࢓૊Έ func allowedChildrenForUnwinding(from source: ɹɹUIStoryboardUnwindSegueSource) -> [UIViewController] func childContaining(_

    source: ɹɹUIStoryboardUnwindSegueSource) -> UIViewController?
  49. Custom Segueͷੈք

  50. Storyboardͳ͠Ͱ࢖͑Δʁ

  51. Storyboardͳ͠Ͱ࢖͑Δʁ class PresentSegue: UIStoryboardSegue { override func perform() { source.present(destination,

    animated: true) } } // SegueΛ࢖͏෦෼ let dest = UIViewController() let segue = PresentSegue( identifier: nil, source: self, destination: dest ) segue.perform()
  52. Custom Segue͸ ͳΜͰ΋ΞϦ

  53. Custom Segue͸ ͳΜͰ΋ΞϦ override func perform() { print(“hello world :)”)

    }
  54. Custom Segue͸ ͳΜͰ΋ΞϦ class XibSegue: UIStoryboardSegue { let nibName: String

    override func perform() { let nib = UINib(nibName: nibName, bundle: nil) let objects = nib.instantiate(withOwner: nil) for case let dest as UIViewController in objects { source.present(dest, animated: true) break } } init(source: UIViewController, nibName: String) { // লུ } }
  55. ΋͏গ࣮͠༻తͳྫ

  56. Static TableView͔ΒͷopenURL

  57. Static TableView͔ΒͷopenURL

  58. Static TableView͔ΒͷopenURL override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

    { switch indexPath.section { case 2: switch indexPath.row { case 0: openURL(“Helpϖʔδ”) default: break } default: break } }
  59. Static TableView͔ΒͷopenURL class OpenURLSegue: UIStoryboardSegue { var url: URL {

    //লུ } override func perform() { // ؆ུԽ͍ͯ͠·͢ UIApplication.shared.open(url) } }
  60. Static TableView͔ΒͷopenURL class OpenURLSegue: UIStoryboardSegue { var url: URL {

    //লུ } override func perform() { // ؆ུԽ͍ͯ͠·͢ UIApplication.shared.open(url) } } final class OpenHelpPageSegue: OpenURLSegue { override var url: URL { return Constant.helpPageURL } }
  61. Static TableView͔ΒͷopenURL OpenHelpPageSegue ☺

  62. ڞ௨ͷNavigationControllerͰϥοϓ

  63. ڞ௨ͷNavigationControllerͰϥοϓ @IBSegueAction func createFoo(_ coder: NSCoder) -> UIViewController? { return

    FooViewController(coder: coder, foo: foo) }
  64. ڞ௨ͷNavigationControllerͰϥοϓ @IBSegueAction func createFoo(_ coder: NSCoder) -> UIViewController? { let

    navi = // UINavigationController(coder: coder) ??? let dest = // FooViewController(coder: coder, foo: foo) ??? ????????? }
  65. ڞ௨ͷNavigationControllerͰϥοϓ class PresentWithNavigationSegue: UIStoryboardSegue { override func perform() { let

    navi = // Storyboard͔ΒNavigationControllerΛload navi.viewControllers = [destination] source.present(navi, animated: true) } }
  66. ڞ௨ͷNavigationControllerͰϥοϓ ☺

  67. ڞ௨ͷNavigationControllerͰϥοϓ ☺ @IBSegueAction func createFoo(_ coder: NSCoder) -> UIViewController? {

    return FooViewController(coder: coder, foo: foo) }
  68. SegueͷՄೳੑ͸ ∞

  69. Wrap-Up ✓Segue͸ը໘ભҠ͚ͩͰ͸ͳ͍ ✓iOS 13͔Β͸Segue ActionͰViewControllerͷinitializer Λࣗ෼Ͱݺ΂Δ ✓Custom SegueͰՄೳੑ͸ແݶେ

  70. ॴ ༑ଠ / Yuta Tokoro @tokorom tokorom More Information https://www.tokoro.me/

  71. Ҿ༻ • https://developer.apple.com/ • WWDC 2019 Videos • https://www.apple.com/