-
-
Save deepakraj27/7a3a3621b4150ec1185a4f951dfb891e to your computer and use it in GitHub Desktop.
Example of organizing UIViewController class with the help of extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// | |
// Created by Dejan Atanasov on 01/04/2017. | |
// Copyright © 2017 Dejan Atanasov. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
//MARK: Private Properties | |
@IBOutlet fileprivate weak var titleLbl: UILabel! | |
@IBOutlet fileprivate weak var actionBtn: UIButton! | |
@IBOutlet fileprivate weak var collectionView: UICollectionView! | |
//MARK: Internal Properties | |
var title: String! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupUI() | |
} | |
} | |
//MARK: Private extension - used to store private methods | |
private extension ViewController{ | |
/* do any UI related stuff here */ | |
func setupUI(){ | |
self.title = "Some Title" | |
} | |
/* keep your actions in this block as well */ | |
@IBAction func someAction(btn: UIButton){ | |
} | |
} | |
//MARK: UICollectionViewDelegate | |
extension ViewController: UICollectionViewDelegate{ | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return 10 | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell | |
return cell | |
} | |
} | |
//MARK: UICollectionViewDataSource | |
extension ViewController: UICollectionViewDataSource{ | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
} | |
} | |
//MARK: UICollectionViewDelegateFlowLayout | |
extension ViewController: UICollectionViewDelegateFlowLayout{ | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
return collectionView.frame.size | |
} | |
} | |
//MARK: SomeCustomDelegate | |
extension ViewController: SomeCustomDelegate{ | |
func customDelegateMethod() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment