0

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()
DanK7
  • 13
  • 5
  • The `gebieteTableView` is a nil. Are you using storyboard? Post your code about the initialization of the `GebieteViewController`. – Bannings Aug 16 '15 at 13:06
  • @Bannings do you mean the initialization in the `NewGebietViewController`? – DanK7 Aug 16 '15 at 13:13
  • Yeah, I think that maybe your initialization is incorrect. Again, are you using storyboard? – Bannings Aug 16 '15 at 13:17
  • Yes i use Storyboard, but i made my own TableViewController. I put a TableView into a ViewController, i also set the delegate and DataSource. I must do this while i want a button over the TableView. – DanK7 Aug 16 '15 at 13:31
  • OK, how did you initialize the `GebieteViewController` in the `NewGebietViewController`? – Bannings Aug 16 '15 at 13:34
  • in the new lines of code you can see i named it `targetVC`. It´s the last line in my question. – DanK7 Aug 16 '15 at 13:38
  • Check my answer. Let me know if that works :) – Bannings Aug 16 '15 at 13:49

1 Answers1

0

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
}()
Community
  • 1
  • 1
Bannings
  • 10,376
  • 7
  • 44
  • 54