How to push a navigation view using collectionView didSelectItemAt? - ios

I am writing a code for my school project. As a beginner would, I looked up on tutorials on youtube how to code.
To summarize the hierarchy of my used objects:
Main collectionView (first one loaded) for scrolling horizontally between views.
List collectionView for listing cells.
collectionViewCell cells for listing information.
However, I am unable to find a way so that when I call a didSelectItemAt function by tapping on one of the cells to push a new view. I have tried creating a function that pushes the view in the MainViewController and call it by creating an instance of the class in didSelectItemAt function but I had no luck.

have you added datasource and delegate?. if you added please check cell click working or not. if click is working add bellow navigation code.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let controller = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(controller, animated: true)
}

Contributing to Raj's answer, if you want to push a view controller when selecting some specific item in your collection view, just a slight variation of condition will do the trick, this delegate method is called with you click on an item in your collection view:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == **<your item's index>** {
let controller = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(controller, animated: true)
}
}

Add the upper Side the Class like this :
class tableViewVC : UIViewController, UITableViewDelegate, UITableViewDataSource {
}
Add inside ViewDidLoad():
tableView.delegate = self
tableView.datasource = self
And then you will use in TableView Method like this :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let ViewController = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
self.navigationController?.pushViewController(ViewController!, animated: true)
}

Related

How to pass to the SeconViewController as the list must show broad list of text in the FirstViewController

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let category = result?.data[indexPath.row]
let vc = ListViewController(items: result!.data)
vc.title = category?.title
vc.navigationController?.pushViewController(vc, animated: true)
}
ListViewController is the second Vc as i want to show category of result in there
First Vc works when it opens up but when i click the cell of tableview it doesnt go to ListViewController.
Thank you in advance

Passing data from one collectionView to other collectionView

In a UICollectionView, I am displaying images and on clicking the image it opens up in a ViewController. Works fine.
However, I want to swipe the images to right/left when the image opens in the new viewController.
To swipe the images, I tried adding a CollectionView in the DetailViewController and added UIImageView inside the cell.
//on clicking the image from main view controller
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController
vc?.name = imgArr[indexPath.row]
self.navigationController?.pushViewController(vc!, animated: true)
}
The error is:
"Illegal Configuration: The img outlet from the DetailViewController
to the UIImageView is invalid. Outlets cannot be connected to
repeating content."
the easiest way for you will be to pass the whole imgArr as a data source for the collectionView in the DetailViewController. Then define a selectedName property in the DetailViewController and scroll to the selected name before DetailViewController has appeared.
EDIT:
//on clicking the image from main view controller
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController {
vc.imgArr = imgArr
vc.selectedNameIndex = imgArr[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
}

UITableView Cell takes a few taps to present view controller

Essentially, with the code below I have the user select on any particular row and have another ViewController shown. While this does work correctly when the app first launches, after pressing and having the ViewController present a few times successfully, so often I will have to press on the cell 2 or 3 times before the ViewController is shown.
Does this have something to do with my custom UITableViewCell class?
The following the code I am using in the TableViewController
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let portfolio = structure[indexPath.row]
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "scheduledDelivery")
}
}
End of didSelectRowAt do
tableView.deselectRow(at: indexPath, animated: false)

navigationController?.pushViewController fails to launch the view

Language is Swift 4
I have a tableView which is showing some categories and below each category it has one or more detail rows. When I click on the detail row, I want it to launch another tableView to show the selected detail row, along with some additional information pertaining to the selected detail row.
To this end, I have implemented the following function:
// launch the detail view controller
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let detailView = DetailVC()
detailView.customInit(detailID: tempViewData[indexPath.section].detailIDs[indexPath.row])
setupTableView.deselectRow(at: indexPath, animated: true)
self.navigationController?.pushViewController(detailView, animated: true)
}
However, when I run the app, while no errors are listed, the detail view does not launch. What am I doing wrong?
Select your first viewController in Storyboard, then embed it into a UINavigationController from the menu like this.
Use self.storyboard?.instantiateViewController(withIdentifier:kCreateFBAccountVCID ) as! DetailVC Instead of DetailVC()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let detailView = **self.storyboard?.instantiateViewController(withIdentifier:kCreateFBAccountVCID ) as! DetailVC**
detailView.customInit(detailID: tempViewData[indexPath.section].detailIDs[indexPath.row])
setupTableView.deselectRow(at: indexPath, animated: true)
self.navigationController?.pushViewController(detailView, animated: true)}

unwrapping a function swift 3

For the life of me I cannot figure out why I can't get this function to work in response to an indexPath being selected.
When I add a breakpoint to the line in didSelectItemAtIndexPath it breaks but when I add one where it function is in the viewscontroller class it won't break. It's as if the information is being sent but cannot reach its destination. I get no error in my console and I've looked over everything multiple times.
Could this be an xcode bug or have I missed something?
Here is where I reference the class it's located in
var viewsController: ViewsController?
Here's my didSelectItemAtIndexPath
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
viewsController?.scrollToMenuIndex(indexPath.item)
print(indexPath.item)
}
And here is the function itself
func scrollToMenuIndex(_ menuIndex: Int) {
let indexPath = IndexPath(item: menuIndex, section: 0)
collectionView?.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition(), animated: true)
}
Your viewsController is declared but not initialized. So it is unknown which class method is called.
var viewsController: ViewsController?
if scrollToMenuIndex is in same class just scrollToMenuIndex(indexPath.item) would be enough !! If its not in same view controller, you need to initialize viewsController as
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let viewsController= storyboard.instantiateViewController(withIdentifier: "someViewController")
viewsController?.scrollToMenuIndex(indexPath.item)
print(indexPath.item)
}
Hope it helps. Happy Coding!!
The reference while valid was returning nil for viewsController.
So in the closure that referenced the collection view that housed the cells that I was using for my didSelectItemAtIndexPath (located in viewsController),
I had to add c.viewsController = self and that fixed it.
I'll have to take a look at what the documentation says about this. I'll update my answer then if no one has yet to explain this behavior.

Resources