Rudest Buddhist Rudest Buddhist - 3 months ago 6
iOS Question

What is the correct pattern to display UITableView data in a UITabBarController?

I'm trying to display some (hard coded for now) data in a UITableView that is inside a UITabBarController, but I have having difficulty filling the rows.

Here is the code for my data object (Reservation.swift)

import UIKit

class Reservation: NSObject {
var tailNumber: String
var aircraftType: String
var arrivalTime: String
var eta: String

init(tailNumber: String, aircraftType: String, arrivalTime: String, eta: String) {
self.tailNumber = tailNumber
self.aircraftType = aircraftType
self.arrivalTime = arrivalTime
self.eta = eta


Here is the code to my Reservation Store (ReservationStore.swift)

import UIKit

class ReservationStore {
// Store all our schedules
var allReservations = [Reservation]()

func createReservation() -> Reservation {
let newReservation = Reservation(tailNumber: "N53440", aircraftType: "C172S", arrivalTime: "07-06-2016 07:00am", eta: "1:58")


return newReservation

init() {

and here is my UITableView view controller (ScheduleViewController.swift)

import UIKit

class ScheduleViewController: UITableViewController {
var reservationStore: ReservationStore!

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reservationStore.allReservations.count

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell", forIndexPath: indexPath)
let item = reservationStore.allReservations[indexPath.row]

cell.textLabel?.text = item.tailNumber

return cell

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let reservationStore = ReservationStore()

let tabController = segue.destinationViewController as! UITabBarController
let reservationController = tabController.viewControllers![1] as! ScheduleViewController

reservationController.reservationStore = reservationStore

I'm receiving a fatal error: unexpectedly found nil while unwrapping an Optional value on return reservationStore.allReservations.count

I believe that prepareForSegue is called as the user clicks the UITabController item for the schedule and right before the view loads. This should trigger the init() in my ReservationStore and create one element in the array but I don't believe that is happening because it's nil by the time the array is counted?

Maybe this creation should be in the prepareForSegue? Been messing with this for a day or so not sure where to head next, could someone point me in the right direction?

Jim Jim

prepareForSegue() is not called before the view loads. It's called when that view controller is preparing to segue to a new view controller. There's nothing assigning to your ScheduleViewController's reservationStore property, so it's nil, hence the crash.

If all you want to do is have an instance of ReservationStore assigned to that property, you can just set it as the default value when defining the property:

class ScheduleViewController: UITableViewController {
    var reservationStore = ReservationStore()

    // …