fatal error after reloadData UITableView in other class

205 Views Asked by At

I'm very new to programming and have a big problem, I spent many hours to solve it. But I don't understand why this is going wrong.

I have a ViewController with a TableView and a another ViewController wheres a View you can add data to the TableView. From this class I call a func from the first ViewController class, in this func is the reloadData. And here starts the problem. The app stops, and this only when I call the func in the second class. When I call it in the first class never a problem appears.

Here is the first class:

class GebieteViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

Here is the func:

func ladeDaten(){

    let request = NSFetchRequest(entityName: "Territory")
    do {
        territorys = try context.executeFetchRequest(request) as! [Territory]
    } catch let gebieteError as NSError {
        print("error: \(gebieteError.localizedDescription)")
    }
    gebieteTableView.reloadData()

}

Here is the second class:

class NewGebietViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate{

Here is the func:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    let neuesGebiet = NSEntityDescription.insertNewObjectForEntityForName("Territory", inManagedObjectContext: self.targetVC.context) as! Territory
    neuesGebiet.territoryName = self.newGebietNameTextField.text
    do {
    try self.targetVC.context.save()
    }catch let gebieteError as NSError {
        print("error: \(gebieteError.localizedDescription)")
    }
    self.targetVC.ladeDaten()
    dismissViewControllerAnimated(true, completion: nil)
    return true
}

A picture that shows the error

https://i.stack.imgur.com/qUo3e.png

Thank you for your help.

More code from the first class:

class GebieteViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

@IBOutlet weak var gebieteTableView: UITableView!
@IBOutlet weak var menuButton: UIBarButtonItem!
@IBOutlet weak var newGebietButton: DesignableButton!
var territorys: [Territory]!

More code from the second class:

class NewGebietViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate{

@IBOutlet weak var newGebietNameTextField: UITextField!

let targetVC = GebieteViewController()
1

There are 1 best solutions below

11
On BEST ANSWER

You should replace this line:

let targetVC = GebieteViewController()

with:

lazy var targetVC: GebieteViewController = {
    let targetVC = self.storyboard?.instantiateViewControllerWithIdentifier("YourIdentifier") as! GebieteViewController
    return targetVC
}()

Using the storyboard id you set in interface builder to create and return the view controller.

Where can I find the name of my "Identifier"?

See also:


UPDATED:(after seeing the project)

The problem is that you initialized the GebieteViewController but you did not display it so the tableView is nil. That means the loadView and viewDidLoad will not be execute in the targetVC.

BTW, you don't need to create a new GebieteViewController because the NewGebietViewController was presented via Modally, so just get it, then you can call the reloadData of the existed GebieteViewController instance.

To fix this just use these code:

lazy var targetVC: GebieteViewController = {
    let targetVC = (self.presentingViewController as! UINavigationController).topViewController as! GebieteViewController
    return targetVC
}()