After reviewing the project I think the best thing to do is to quickly review the options available. In prepareforSegue you may want to do one of two things:
- Assign the value(s) of registreringsnummer to a variable on your second tableview controller (not an IBOutlet, just a var) ; then build the fetchrequest using that variable after viewDidLoad
- Create the fetch and pass the results to an array variable on your second tableview controller (not an IBOutlet, just a var); then use the array values after viewDidLoad
In your uploaded project I did not find an attribute "fordon" on your entity, so assigning anything to a non-existent attribute will not work. Either update your model with a valid attribute OR test the query process using an existing attribute like kronor, liter, or literpris as a test to confirm that the basic functionality works.
The test would be to simply choose one of the methods above and within either your second view controller run the fetch request on the value provided (as described above).
OR
You can build a fetch request with a predicate and pass the array of results to the second controller. (sample code provided below)
Important ~ Put a break point in your code so that you can validate the variable, predicate, and fetch results before they are passed between the first tableview controller and the second tableview controller.
To make the values available on the second tableviewcontroller just add the variables.
class secondTableViewController:UITableViewController {
var registreringsnummer: Float = 0.0 //I did not see this value on your model
...
}
//Note - I would handle a string search very differently since it does not have good
// performance using equality operators
// Check this link for other predicate operators
//You must create a variable to hold the objects you will use in the second tableview controller
class secondTableViewController:UITableViewController {
var fetchResults = [Tankningslista]()
..
}
To send the values from the first tableviewcontroller you need to setup a data reference to hold the outgoing objects.
class tankningarTableViewController: UITableViewController, NSFetchedResultsControllerDelegate {
...
var fetchedResultsController: NSFetchedResultsController? = nil
let context = (UIApplication.sharedApplication().delegate asAppDelegate).managedObjectContext!
...
In your tankningarTableViewController you will want to prepare the data and assign it to the variable on your secondTableViewController
override func prepareForSegue {
...
if let secondTableViewController = segue.destinationViewController as? SecondTableViewControllerClassName {
secondTableViewController.fetchedResults = self.fetchTankningslistaObjects (2.0)
...
}
This function will perform the fetch and return the objects that you can then assign to your second tableview controller (in the prepareForSegue)
// typical example of returning an array of objects from a fetch with a predicate argument
// you could change the argument to different values/types as needed
// check that the predicate and argument values are correct by adding a breakpoint into the function
func fetchTankningslistaObjects (argument:Float)->[Tankningslista] {
//get your fetch results prepared to send to the second tableviewcontroller as an array
var fetchRequest:NSFetchRequest = NSFetchRequest(entityName: "Tankningslista")
fetchRequest.fetchLimit = 25
let sortDescriptorsArray = [NSSortDescriptor(key: "date", ascending: false)]
let aPredicate = NSPredicate(format: "kronor = %@", argument)
// if kronor is an attribute and argument is a valid float your predicate should work when applied to a Modeled entity with the same attribute name and argument type, change the attribute name and matching argument type as needed
fetchRequest.sortDescriptors = sortDescriptorsArray
fetchRequest.predicate = aPredicate
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.context sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
do
{
try self.fetchedResultsController!.performFetch()
} catch let e as NSError {
/
fatalError(e.localizedDescription)
}
return self.frc!.fetchedObjects as? [Tankningslista]
}
This is written off the top of my head. You will probably need to find and fix small optionality errors. I noticed that your existing code is Swift 1.2.
Important - Please update to the latest version of Xcode if possible and convert to Swift 2.0.
Hopefully this resolves your issue and gives a good idea of how to move the data between the tableview controllers. You still need to setup the tableview controllers to populate correctly using the array of objects. If this helps please click the link below so that others can find this solution. Otherwise update the thread.