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

Change Storyboard to Swift

C58d9fc209cfdc0b822f851911110fa6?s=47 coe
July 15, 2019

Change Storyboard to Swift

Change Storyboard to Swift.

C58d9fc209cfdc0b822f851911110fa6?s=128

coe

July 15, 2019
Tweet

Transcript

  1. 4UPSZCPBSE೿͕ ίʔυͰॻ͘ͱ͜͏ͳΔ גࣜձࣾ"NB[JB೔޲ڧ

  2. ࣗݾ঺հ w ೔޲ڧʢ5TVZPTIJ)ZVHBʣ w J1IPOF04ʙ w "OESPJE%POVUʙ w UW04ʙ

  3. None
  4. 4UPSZCPBSE೿Ͱ͢

  5. ͚ͲɺίʔυͰ͔ॻ͔ͳ͍ͱ ͍͚ͳ͍࣌ͬͯ͋Γ·͢ΑͶ

  6. ίʔυͰॻ͘

  7. class TmpViewController: UIViewController { let label = UILabel() let button

    = UIButton() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white label.text = "Hello World" button.setTitle("button", for: .normal) view.addSubview(label) view.addSubview(button) } }
  8. ͜ͷॻ͖ํͩͱɺ 4UPSZCPBSEͰग़དྷͨ ࢖͍ํ͕Ͱ͖ͳ͍

  9. ྫɿ஍ҬΛλοϓͯ͠ ৘ใΛදࣔ͢Δ ೔ຊ஍ਤΞϓϦͷ৔߹

  10. /BWJHBUJPO

  11. .PEBM ด͡ΔϘλϯ͕ඞཁʂ

  12. 'PSN4IFFU ਖ਼ํܗͳͷͰɺ ԭೄΛผ࿮ʹͨ͠ σβΠϯʹ͍ͨ͠ʂ

  13. UW04 Ϙλϯʹ͸57$BQUJPO#VUUPO7JFXΛ࢖͍͍ͨʂ

  14. 4UPSZCPBSEͷ৔߹ w ͜ΕΒશͯͷ7JFXʹରͯ͠ಉ Ұͷ7JFX$POUSPMMFSΛׂΓ౰ ͯΔ͜ͱͰɺ7JFX$POUSPMMFS ͸ॲཧ͚ͩʹઐ೦͢Δ͜ͱ͕ Ͱ͖Δ

  15. ͜Ε͕ࠔΔΑίʔυϕʔε w 7JFX$POUSPMMFSʹ7JFXͷσβΠϯΛ͍ͯ͠Δ w ʮσβΠϯ͕ҧ͏͚ͩͰɺ΍Δ͜ͱ͸ಉ͡ʯͳ 7JFX$POUSPMMFSΛԿݸ΋༻ҙͨ͠Γɺϝϯςφϯε͠ ͍͔ͯͳ͍ͱ͍͚ͳ͍ w ͍ͨ΁Μ

  16. 4UPSZCPBSEͱಉ༷ͷ͜ͱ͕ Ͱ͖Δ 7JFX$POUSPMMFSΛ࡞Δ

  17. 7JFX'BDUPSZύλʔϯ ʢԾশʣ

  18. protocol CodeViewFactoryOwner: class { var titleLabel: UILabel! { get set

    } var captionLabel: UILabel! { get set } var imageView: UIImageView! { get set } } class CodeViewFactory: CodeViewFactoryType { func create(owner:CodeViewFactoryOwner) -> UIView { return MyCodeView(owner: owner, frame: .zero) } } class MyCodeView: UIView { init(owner:CodeViewFactoryOwner, frame: CGRect) { super.init(frame: frame) self.backgroundColor = .white let imageView = UIImageView() let titleLabel = UILabel() let captionLabel = UILabel() let button = UIButton(type: .system) button.setTitle("Play", for: .normal) button.addTarget(owner, action: Selector(("clickPlay:")), for: .touchUpInside) self.addSubview(imageView) self.addSubview(titleLabel) self.addSubview(captionLabel) self.addSubview(button) owner.imageView = imageView owner.captionLabel = captionLabel owner.titleLabel = titleLabel } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } }
  19. 7JFX'BDUPSZύλʔϯ w ֤छ6*ύʔπΛ࣋ͬͨ0XOFSϓϩτίϧΛ࡞੒͢Δ w 7JFX'BDUPSZ͸0XOFSϓϩτίϧΛड͚औΓɺ7JFXͷ࡞੒ Λߦ͏ w 0XOFSʹ֤छ6*ύʔπΛ౉͢ w #VUUPOͳͲͷBEE5BSHFUઌʹ0XOFSΛ౉͢

    w μʔΫϞʔυ΍4J[FDMBTTFTʹରԠ͢Δ৔߹ɺద౰ͳ6*7JFX ܧঝΫϥεΛ࡞ΓɺUSBJU$PMMFDUJPO%JE$IBOHF౳Ͱௐ੔
  20. class CodeViewController: UIViewController,CodeViewFactoryOwner { @IBOutlet weak var titleLabel: UILabel! @IBOutlet

    weak var captionLabel: UILabel! @IBOutlet weak var imageView: UIImageView! private let viewFactory:CodeViewFactoryType? init(viewFactory:CodeViewFactoryType) { self.viewFactory = viewFactory super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { viewFactory = nil super.init(coder: aDecoder) } override func loadView() { if let viewFactory = viewFactory { view = viewFactory.create(owner: self) } else { super.loadView() } } @IBAction func clickPlay(_ sender: Any) { } }
  21. 7JFX'BDUPSZύλʔϯ 7JFX$POUSPMMFSฤ w 7JFX$POUSPMMFSΛ0XOFSϓϩτίϧʹద߹ͤ͞Δ w 7JFX$POUSPMMFSͰૢ࡞͍ͨ͠7JFXΛXFBLWBSͰϓϩύςΟఆٛ w ͔ͤͬ͘ͳͷͰɺ!*#0VUMFU΋͚ͭΔ w 7JFX͔Βड͚औΔΞΫγϣϯΛGVODఆٛ

    w ͔ͤͬ͘ͳͷͰɺ!*#"DUJPO΋͚ͭΔ w JOJU࣌ʹ೚ҙͷ7JFX'BDUPSZΛड͚औΔ w MPBE7JFX ͰɺTFMGWJFXʹ7JFX'BDUPSZ͔Β࡞ͬͨ7JFXΛׂΓ౰ͯΔ
  22. 7JFX'BDUPSZύλʔϯ ϝϦοτ w 7JFX$POUSPMMFS͕7JFXͷίʔυͰ๲Β·ͳ͍ w ෼ذͱ͔JT)JEEFOͱ͔࡞Βͳ͍͍ͯ͘ w 7JFX$POUSPMMFSΛ৭ʑͳγʔϯͰ࢖͍·ΘͤΔ w 7JFX$POUSPMMFS͕ɺͲ͏͍͏7JFXσβΠϯʹͳ͍ͬͯΔ͔ؾʹ͠ͳͯ͘

    ͍͍ w 6*#VUUPOͩΖ͏ͱɺ57$BQUJPO#VUUPO7JFXͩΖ͏ͱɺΠϯλϥΫ γϣϯΛॲཧ͢Δ͚ͩ w ίʔυϕʔεͱ9JCϕʔεͱ4UPSZCPBSEϕʔεΛ؆୯ʹ੾Γସ͑ΒΕΔ
  23. 9JCͷ৔߹ͷ7JFX'BDUPSZྫ class XibViewFactory: CodeViewFactoryType { func create(owner:CodeViewFactoryOwner) -> UIView {

    let nib = UINib(nibName: "CodeXibView", bundle: nil) let xibview = nib.instantiate(withOwner: owner, options: nil) return xibview.first as! UIView } } ˞YJCϑΝΠϧͷ'JMF`TPXOFSͷ$MBTTʹ7JFX$POUSPMMFSΛׂΓ౰͓ͯͯ͘͜ ͱ
  24. 4UPSZCPBSEͷ৔߹ override func loadView() { if let viewFactory = viewFactory

    { view = viewFactory.create(owner: self) } else { super.loadView()// ←͜͜ } } 4UPSZCPBSEͷ৔߹ɺWJFX'BDUPSZ͸OJMͳͨΊɺ4UPSZCPBSEͷઃఆͰ6*͕ ߏங͞ΕΔ
  25. ·ͱΊ w 4UPSZCPBSEͱಉ͡ײ͡Ͱ7JFX7JFX$POUSPMMFSΛ࡞Γͨ ͍৔߹ɺ w 7JFX$POUSPMMFSͷ֎͔Β7JFXΛJOKFDU͢Δ w ΧελϜ7JFX͸ɺMPBE7JFX ͰɺTVQFSMPBE7JFX Λݺ͹ͣʹTFMGWJFXʹઃఆ͢Δ

    w IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPO VJLJUVJWJFXDPOUSPMMFSMPBEWJFX
  26. if #available(iOS 13.0, *) { self.backgroundColor = .systemBackground } else

    { self.backgroundColor = .white } Ͱ΋ɺ͜͏͍͏04ؒͷΘͣΒΘ͠͞Λແͨ͘͢Ίʹ΋ɺ 4UPSZCPBSE༗ޮͰ͢Αɻ ʢ4UPSZCPBSE্Ͱ4ZTUFN#BDLHMPVE$PMPSΛઃఆ͢Δ͚ͩͰ શͯͷ04ʹରԠͰ͖Δʣ
  27. גࣜձࣾ"NB[JBͰ͸ ΤϯδχΞΛืू͍ͯ͠·͢ IUUQTBNB[JBDPKQDBSFFS