Skip to content

Instantly share code, notes, and snippets.

@zorn
Created May 4, 2015 21:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zorn/c20d7ddddf06ac3f5c61 to your computer and use it in GitHub Desktop.
Save zorn/c20d7ddddf06ac3f5c61 to your computer and use it in GitHub Desktop.
UIViewController pseudo code
import UIKit
class UIViewController : NSObject {
var _view: UIView?
var view: UIViewb = {
if isViewLoaded() == false {
loadView()
viewDidLoad()
}
return _view!
}()
func loadView() {
// load from a xib into view
// first check if the property nibName is set else,
// check if we have a nib with the same name as the class (Swift issues)
// load nib, else
// load a generic blank UIView
}
func viewDidLoad() {
// Do things like use outlets to finish preparing the UI
}
func isViewLoaded() -> Bool {
let answer:Bool = false
return answer
}
}
// NOTES: If you ever need to ask a ViewController if
// the view is loaded use `isViewLoaded`
class BNRProgramaticViewcontroller : UIViewController {
override func loadView() {
let newView = UIView(frame: CGRect()) // will let someone else determine this
newView.backgroundColor = UIColor.redColor()
self.view = newView;
let newLabel = UILabel(frame: CGRect())
let newButton = UIButton(frame: CGRect())
self.view.addSubview(newLabel)
self.view.addSubview(newButton)
newButton.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
}
func buttonAction(sender: AnyObject?) {
println("button tapped!")
}
}
// One way to make a nib based
let quizVC = QuizViewController(nibName: "QuizViewController", bundle: nil)
class BNRNibBasedViewController : UIViewController {
convenience init() {
self.init(nibName: "BNRNibBasedViewController", bundle: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment