I have a VC1 with a UITableView which populate it cells with a NSFetchedResultsController. I want VC1 tableView to place all new added cells to the bottom of tableView, therefore I created a CoreData attribute: itemNumberForVC1
(type Int32). The cells can be added only from VC2. That's why I need to pass the numberOfObjects
from VC1 method numberOfRowsInSection
to VC2.
The numberOfObjects
is in the following method of VC1:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let numberOfObjects = fetchedResultsController.sections?[section].numberOfObjects else {return 0}
return numberOfObjects
}
I need to use the numberOfObjects
in my VC2 didSelectRowAt
method:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currency = fetchedResultsController.object(at: indexPath)
currency.isForConverter = !currency.isForConverter
//currency.itemNumberForVC1 = numberOfObjects - 1 <- calculation: there I should receive the actual
//number of objects from VC1 and assign the number to picked currency, and it should appear in VC1
//at the bottom of its TableView
coreDataManager.save()
}
I tried to use delegates but inside VC2 didSelectRowAt
the result is always 0. The max result I've got is created a method in VC2, which takes numberOfObjects
as a parameter, call it in VC1 and then I have that number inside the VC2 but only in borders of the method. I can't move it to VC2 didSelectRowAt
(tried through a global variable).
How can I do that efficiently?
Have you tried to simply pass the value when you push or present the VC2 view controller?
For example in you VC2 declare a variable like this:
class VC2: UIViewController {
...
var numberOfObjects: Int = 0
...
}
Then in you VC1, when you present or push the VC2 you should pass the value:
let vc2 = <get your VC2 instance here>
vc2.numberOfObjects = <your value here>
<present or push your vc2 here>