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

Change Storyboard to Swift

coe
July 15, 2019

Change Storyboard to Swift

Change Storyboard to Swift.

coe

July 15, 2019

Transcript

27 slides
  1. 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) } }
  2. 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) } }
  3. 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) { } }
  4. 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ϕʔεΛ؆୯ʹ੾Γସ͑ΒΕΔ
  5. 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ΛׂΓ౰͓ͯͯ͘͜ ͱ
  6. 4UPSZCPBSEͷ৔߹ override func loadView() { if let viewFactory = viewFactory

    { view = viewFactory.create(owner: self) } else { super.loadView()// ←͜͜ } } 4UPSZCPBSEͷ৔߹ɺWJFX'BDUPSZ͸OJMͳͨΊɺ4UPSZCPBSEͷઃఆͰ6*͕ ߏங͞ΕΔ
  7. if #available(iOS 13.0, *) { self.backgroundColor = .systemBackground } else

    { self.backgroundColor = .white } Ͱ΋ɺ͜͏͍͏04ؒͷΘͣΒΘ͠͞Λແͨ͘͢Ίʹ΋ɺ 4UPSZCPBSE༗ޮͰ͢Αɻ ʢ4UPSZCPBSE্Ͱ4ZTUFN#BDLHMPVE$PMPSΛઃఆ͢Δ͚ͩͰ શͯͷ04ʹରԠͰ͖Δʣ

More Decks by coe

Other Decks in Technology