Reputation: 173
I have a tabBarController
and two UINavigationControllers
with UITableViews
attached to the tabBarController
. I have subclassed the tabBarController
so I can pass an array of custom objects between the two tables by reference. However, when I go to populate the array, the tableView
outlet is unwrapping to nil and I am getting the error "fatal error: unexpectedly found nil while unwrapping an Optional value". Not sure why this is happening. Below is the code:
tabBarController
protocol CustomTabBarDelegate {
var placeDataArray : Array <placeData> {get set}
}
class placeData: Equatable {
var description : String
var selected : Bool
init (description : String, selected : Bool) {
self.description = description
self.selected = selected
}
}
class PlacesTabBarController: UITabBarController {
var placeDataArray = Array<placeData>()
override func viewDidLoad() {
super.viewDidLoad()
placeDataArray = [placeData(description: "Afghanistan", selected: false), placeData(description: "Albania", selected: false)]
var table1 = AllPlacesViewController()
var table2 = AttendingPlacesTableViewController()
table1.delegate = self
table2.delegate = self
var navController1 = UINavigationController(rootViewController: table1)
var navController2 = UINavigationController(rootViewController: table2)
self.viewControllers = [navController1, navController2]
}
}
tableViewControllers
var delegate:PlacesTabBarController!
and then, for instance, I can access the array by calling
delegate.placeDataArray.count
When I go to populate the table with
@IBOutlet weak var allPlacesTableView: UITableView!
and use
var cell = self.allPlacesTableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
The error mentioned above is thrown. Not sure why allPlacesTableView
is unwrapping to nil
Upvotes: 1
Views: 1740
Reputation: 1208
Probably, You are not instantiating your ViewController properly. make sure if you're instantiating your VC from storybaord you are doing it like this:
let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let someVC = storyboard.instantiateViewController(withIdentifier: "SomeViewController") as? SomeViewController
Upvotes: 0
Reputation: 21536
When you create your VCs like this,
var table1 = AllPlacesViewController()
they are not linked to the storyboard, so the outlets are not fulfilled. Use the instantiateViewControllerWithIdentifier
method of self.storyboard instead:
var table1 : AllPlacesViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("AllPlaces") as AllPlacesViewController
Upvotes: 7