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()