Skip to content

Instantly share code, notes, and snippets.

@jmcd
Created June 18, 2015 07:05
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmcd/d3974e1783279a881f87 to your computer and use it in GitHub Desktop.
Save jmcd/d3974e1783279a881f87 to your computer and use it in GitHub Desktop.
Master Detail with a UISplitViewController without StoryBoards
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = SplitVc()
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.rootViewController = viewController
window.makeKeyAndVisible()
self.window = window
return true
}
}
class SplitVc : UISplitViewController, UISplitViewControllerDelegate {
override func viewDidLoad() {
self.delegate = self
let master = UINavigationController()
let detail = UINavigationController()
master.viewControllers = [TableVc()]
detail.viewControllers = [DetailVc()]
self.viewControllers = [master, detail]
}
func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -> Bool {
if let nc = secondaryViewController as? UINavigationController {
if let topVc = nc.topViewController {
if let dc = topVc as? DetailVc {
let hasDetail = Thing.noThing !== dc.thing
return !hasDetail
}
}
}
return true
}
}
class TableVc : UITableViewController {
override func viewDidLoad() {
self.title = "Things"
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Repo.sharedRepo.allThings.count;
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let thing = Repo.sharedRepo.allThings[indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = thing.name;
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let thing = Repo.sharedRepo.allThings[indexPath.row]
let vc = DetailVc()
vc.thing = thing
let nc = UINavigationController()
nc.viewControllers = [vc]
self.showDetailViewController(nc, sender: self)
}
}
class DetailVc : UIViewController {
var thing : Thing = Thing.noThing
override func viewWillAppear(animated: Bool) {
self.title = thing.name
self.view.backgroundColor = thing.color
if let svc = self.splitViewController {
let ni = self.navigationItem
ni.setLeftBarButtonItem(svc.displayModeButtonItem(), animated: false)
ni.leftItemsSupplementBackButton = true;
}
super.viewWillAppear(animated)
}
}
class Repo {
var allThings : [Thing]
class var sharedRepo : Repo {
struct Static {
static let instance = Repo()
}
return Static.instance
}
init() {
allThings = [
Thing(name: "Red", color: UIColor.redColor()),
Thing(name: "Green", color: UIColor.greenColor()),
Thing(name: "Blue", color: UIColor.blueColor()),
]
}
}
class Thing {
let name : String
let color : UIColor
init(name : String, color : UIColor) {
self.name = name
self.color = color
}
class var noThing : Thing {
struct Static {
static let instance = Thing(name: "No Thing", color: UIColor.grayColor())
}
return Static.instance
}
}
@sebmos
Copy link

sebmos commented May 5, 2019

@thang-nm Insulting somebody who provides a potentially useful snippet of code to the community is unnecessary. If you have concrete suggestions, you're free to provide those, or fork & improve it. Otherwise, calm down & move on.

@taimila
Copy link

taimila commented May 29, 2020

This was very useful. Thanks!

@willwfsp
Copy link

That's great! It helped me a lot. Thank you very much. Just consider reducing the number of spaces of your indentation. It will increase the readability of your code.

Additionally, there are some cool style guides you can follow and improve you code even more. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment