How to parse json when collectionView is tap - ios

Hello I'm just wondering if there is another way to parse json data when a collection view is click and pass it to another view. This is the API im using. This is how I do it at the moment. This is how my class looks. First I create a closure. Then I created my hero class. Then I have my json parse function.
This is how my collectionView didSelectItemAt func looks. This is where im trying to put my json parse function, or is it better to put it in the hero class. Im new to this so I don't really know.
Not sure if I explained it well. So I'll include my viewcontroller code if needed. Would appreciate any help :)

You should create variable for another viewcontroller and pass the data to that variable by using didselect method

After perform Segue you need to do following code to pass the Hero object to HeroDetail Controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Pass the selected object to the new view controller.
if segue.identifier == "HeroDetails" {
let heroDetailVC = segue.destinationViewController as! HeroDetailVC
heroDetailVC.hero = sender as! Hero
}
}
Make sure you have created Variable for Hero Object in HeroDetail Controller.
This will help you to pass the data over multiple controller using Segue Method.

Related

Passing Data Between two ViewControllers in MVVM

I recently started exploring MVVM and stuck at passing data between 2 view controllers.I have an array of items in my viewModel1 of ViewController1 and I want to pass that array to viewcontroller2. I am able to achieve it by getting the whole array from viewModel1 like below but I am not sure if this is the right way to do it.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == Constants.kShowDetailSegue {
let destinationViewController = segue.destination as! ListViewController
destinationViewController.products = viewModel1.getFavouriteProducts()
//the getFavouriteProducts gets data from coredata.
}
}
I think you have done it the right way, but alternatively you can use closures. Which are the preferred, method of communication within iOS. You will take care of memory leaks wherever necessary by using
weak
OR
unowned

How to pass data from ViewControllers and show them into tableviewCell?

I want to pass data from my ViewController to TableViewCell, How can I do that. How to pass data from one ViewController and show them in TableViewCell.
The question is a bit too vague, but I will try. If you are using a segue from another viewController and the data is, for example, an array of Strings i.e in your first viewController:
var arrayOfStrings = ["StringRow1", "StringRow2"] // etc
and you want to be able to access this array when you enter a new viewController and use it for your tableView then you can use prepareForSegue
First, in your viewController with the tableView have a variable ready for the array:
var arrayOfStrings = [String]()
Then you can pass the data over like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SegueToTableView" {
if let vc = segue.destination as? YourTableViewController
{
vc.arrayOfStrings = self.arrayOfStrings
}
}
}
Now when you arrive in your tableView viewController, your arrayOfStrings data will be passed.
This is a basic simple example as you did not provide much context.
Creat variable in your cell class and in cell for row just assign that value to variable and use in your cell class.

I can only add a new cell to a collection View Controller through prepareforSegue

I am trying to add a new cell to my collectionViewController after another controller adds an additional element to the array which serves as the data source for the view controller. Ive been told that it is wrong to do it this way but the only way that I can get the collectionViewController to add a new cell is if I call insertItems(at: [indexPath]) in the prepareForSegue function.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "unwindToCollectionView"{
let CVC = segue.destination as! CollectionViewController
if let image = image{
CVC.images.insert(imagePost(image:image), at: 0)
print("Image is not nil")
}
let index = IndexPath(item: 0, section: 0)
CVC.collectionView?.insertItems(at: [index])
print("In prepare for segue: \(CVC.images.count)")
}
Is this the wrong way to add a new cell, what would be the correct way to implement what I am trying to achieve here? Thank you.
This is not a wrong way , but you better try to make the array global and add items to it everywhere you want and whenever the main VC appear in viewDidAppear reload the collection
If you have CustomCollectionViewController and AddToArrayViewController, create a delegate protocol AddToArrayViewControllerDelegate, with a delegate method addToArrayViewController(viewController: didAdd:).
Whenever your AddToArrayViewController adds something, you call your addToArrayViewController(viewController: didAdd:) delegate call.
Make sure CustomCollectionViewController implements AddToArrayViewControllerDelegate, and then whenever the didAdd call is made, you can add the item to the array, and reload the CollectionView.

Not able to push data from one view controller to a table view controller

I ran into a strange problem while I was working on my project. (Please bear with me, I'm a Swift beginner.)
This is what my storyboard looks like so far.
On the left is my regular view controller, and on the right is the TableViewController. the transition inbetween has a specific ID.
**This is the first View Controller.**
class TestsMenuViewController: UIViewController {
var tests = [Test]()
var testTableViewController : TestsTableViewController?
override func viewDidLoad() {
self.tests = DataModel().patientData
//this just populates the array with some data.
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "testTableTransition" {
testTableViewController = segue.destination as? TestsTableViewController
testTableViewController?.tests = self.tests
print(testTableViewController?.tests[0].testDescription)
//somehow this is an empty array
}
}
somehow the test array does not get passed to the next controller :( what am i doing wrong?
You could save the data as NSUserDefaults and call the saved data to the view when you want
Hey I managed to solve my problem, what I just did was initialize the data that I pass to the TableViewController directly in the attributes. Then it worked!

How to Pass Array Object between view controllers by reference

I am trying to pass an Array Object between 2 view controllers. But I want the Array to be passed as a Reference and not copied. I know if I was using a function locally, I can use "inout" and "&" based on the examples I found.
But I what I want to do is slightly different. I want to assign the Array in VC1 directly to the Array object in VC2 as a reference. Here is my code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "vc2" {
let vc2 = segue.destinationViewController as! ProfileVC
vc2.data = &self.data //I know this line doesn't work, how would I go about it?
}
}
Swift array are struct, so they will get copied.
The simplest way would be to use an object instead of a struct. So I would use a NSMutableArray instead.
With that you will have all the power of mutability at your finger tips ;-)

Resources