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

Advanced Segue 2019年のSegue事情

tokorom
September 05, 2019

Advanced Segue 2019年のSegue事情

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

tokorom

September 05, 2019
Tweet

More Decks by tokorom

Other Decks in Technology

Transcript

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

    tvOS App Programmer #iOSDC Japan 2019 day0 2019/09/05 18:40ʙ Track B
  2. prepareForSegue override func prepare(for segue: …) { if let acceptable

    = segue.destination as? FooAcceptable { if let foo = sender as? Foo { acceptable.accept(foo: foo) } } }
  3. viewDidAppear͕ݺ͹Εͳ͍ύλʔϯ override func prepare(for segue: …) { segue.destination.presentationController?.delegate = self

    } func presentationControllerDidDismiss( _ presentationController: UIPresentationController) { // present͞ΕͨViewController͕ด͡ΒΕͨͷΛݕ஌ }
  4. override func prepare(for segue: …) { if let acceptable =

    segue.destination as? FooAcceptable { if let foo = sender as? Foo { acceptable.accept(foo: foo) } } } SegueΛ࢖͏έʔεʢ͜Ε·Ͱʣ
  5. 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Λ࢖͏έʔεʢ͜Ε·Ͱʣ
  6. 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) } }
  7. 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) } }
  8. 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Λ୅ೖ } } ☺
  9. 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()
  10. 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) { // লུ } }
  11. 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 } }
  12. Static TableView͔ΒͷopenURL class OpenURLSegue: UIStoryboardSegue { var url: URL {

    //লུ } override func perform() { // ؆ུԽ͍ͯ͠·͢ UIApplication.shared.open(url) } }
  13. 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 } }
  14. ڞ௨ͷNavigationControllerͰϥοϓ @IBSegueAction func createFoo(_ coder: NSCoder) -> UIViewController? { let

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

    navi = // Storyboard͔ΒNavigationControllerΛload navi.viewControllers = [destination] source.present(navi, animated: true) } }