Slide 1

Slide 1 text

Advanced Segue 2019೥ͷSegueࣄ৘ ॴ ༑ଠ / Yuta Tokoro, iOS & tvOS App Programmer #iOSDC Japan 2019 day0 2019/09/05 18:40ʙ Track B

Slide 2

Slide 2 text

ॴ ༑ଠ / Yuta Tokoro @tokorom tokorom Spinners Inc.

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Agenda • Basic Segue • iOS 13 New Features! • Advanced Tips •

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Segue ͱ͸ʁ

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Segueͷछྨ • Standard SegueʢViewControllerΛ։͘ʣ • Embed SegueʢViewControllerͷೖΕࢠʣ • Unwind SegueʢಛఆͷViewController·ͰḪΔʣ • Custom SegueʢͳΜͰ΋ΞϦʣ •

Slide 10

Slide 10 text

Standard Segue

Slide 11

Slide 11 text

Embed Segue

Slide 12

Slide 12 text

Unwind Segue ViewController ViewController ViewController ViewController ViewController

Slide 13

Slide 13 text

Custom Segue class AwsomeSegue: UIStoryboardSegue { override func perform() { // ͳΜͰ΋ΞϦ } }

Slide 14

Slide 14 text

SegueΛ࢖͏Pros • StoryboardͰ׬݁Ͱ͖Δ෦෼͕૿͑Δ • prepareForSegueͳͲͰҰݩ؅ཧͰ͖Δ • Apple Way͔Β֎Εͳ͍ •

Slide 15

Slide 15 text

Static TableViewͱͷ૬ੑൈ܈

Slide 16

Slide 16 text

Static TableViewͱͷ૬ੑൈ܈

Slide 17

Slide 17 text

prepareForSegue

Slide 18

Slide 18 text

Segueͷछྨ • Standard SegueʢViewControllerΛ։͘ʣ • Embed SegueʢViewControllerͷೖΕࢠʣ • Unwind SegueʢಛఆͷViewController·ͰḪΔʣ • Custom SegueʢͳΜͰ΋ΞϦʣ •

Slide 19

Slide 19 text

prepareForSegue override func prepare(for segue: …) { if let acceptable = segue.destination as? FooAcceptable { if let foo = sender as? Foo { acceptable.accept(foo: foo) } } }

Slide 20

Slide 20 text

viewDidAppear͕ݺ͹Εͳ͍ύλʔϯ

Slide 21

Slide 21 text

viewDidAppear͕ݺ͹Εͳ͍ύλʔϯ override func prepare(for segue: …) { segue.destination.presentationController?.delegate = self } func presentationControllerDidDismiss( _ presentationController: UIPresentationController) { // present͞ΕͨViewController͕ด͡ΒΕͨͷΛݕ஌ }

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

ViewControllerʹ஋Λ౉͢ ViewController ViewController Foo

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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Λ࢖͏έʔεʢ͜Ε·Ͱʣ

Slide 28

Slide 28 text

class FooViewController: UIViewController { let foo: Foo } ͜͏͍ͨ͠

Slide 29

Slide 29 text

Segue Action

Slide 30

Slide 30 text

Segue Action Source Destination

Slide 31

Slide 31 text

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) } }

Slide 32

Slide 32 text

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) } }

Slide 33

Slide 33 text

Segue Action

Slide 34

Slide 34 text

Segue Action

Slide 35

Slide 35 text

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Λ୅ೖ } } ☺

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Segue ࢖ͬͯ΋͍͍ͷͰ͸ʁ

Slide 39

Slide 39 text

Embed Segue + Segue Action

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Unwind Segueͷੈք

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

ϊϯίʔυͰUnwind

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Custom Segueͷੈք

Slide 50

Slide 50 text

Storyboardͳ͠Ͱ࢖͑Δʁ

Slide 51

Slide 51 text

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()

Slide 52

Slide 52 text

Custom Segue͸ ͳΜͰ΋ΞϦ

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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) { // লུ } }

Slide 55

Slide 55 text

΋͏গ࣮͠༻తͳྫ

Slide 56

Slide 56 text

Static TableView͔ΒͷopenURL

Slide 57

Slide 57 text

Static TableView͔ΒͷopenURL

Slide 58

Slide 58 text

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 } }

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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 } }

Slide 61

Slide 61 text

Static TableView͔ΒͷopenURL OpenHelpPageSegue ☺

Slide 62

Slide 62 text

ڞ௨ͷNavigationControllerͰϥοϓ

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

ڞ௨ͷNavigationControllerͰϥοϓ class PresentWithNavigationSegue: UIStoryboardSegue { override func perform() { let navi = // Storyboard͔ΒNavigationControllerΛload navi.viewControllers = [destination] source.present(navi, animated: true) } }

Slide 66

Slide 66 text

ڞ௨ͷNavigationControllerͰϥοϓ ☺

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

SegueͷՄೳੑ͸ ∞

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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