0

Update, see my comment below, as what I did wrong. Thought someone may benefit from all these newbie mistakes:) : I have a collectionView with section titles and cell titles (UILabels), which is dynamically fed in via cloudkit. I was able to get the code to finally work to select a cell and then send the cell's title to the 2nd ViewController's navigationItem.title property.

However, now the 2nd ViewController is reloading after it appears the first time. I embedded the first CollectionViewController within a navigation controller. And I created a push segue in storyboard from my prototype cell within the CollectionView to the 2nd ViewController, and provided an identifier for the segue. Any idea why it's reloading the 2nd ViewController again after appearing the first time?

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {


     self.performSegueWithIdentifier("selected", sender: indexPath)


}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "selected" {


        let indexPaths : NSArray = self.collectionView!.indexPathsForSelectedItems()
        let indexPath : NSIndexPath = indexPaths[0] as NSIndexPath

        let theSelectedItem = sections[indexPath.section].category[indexPath.item]


        let svc = segue.destinationViewController as TableViewControllerNew
        svc.navigationItem.title = theSelectedItem

        // I created the tableview controller in the storyboard, and then subclassed the UITableViewController, and set the storyboard tableview controller's class to the subclass in the identity inspector
    }
Renee Olson
  • 267
  • 1
  • 15
  • Nevermind! I figured out the problem....newbie mistake! I didn't realize that if you have a segue in the storyboard, you don't need to also call the "performSegueWithIdentifier". Hope this post can help someone else. Also, found an alternate version of the code in the prepareForSegue method, which is shorter. Instead of the two lines for indexPaths and indexPath above,replace with this line: `let indexPath: NSIndexPath = self.collectionView!.indexPathForCell(sender as CollectionViewCell)!` – Renee Olson Dec 18 '14 at 01:51

1 Answers1

0

Set the self.navigationItem.title = ... in the didSelect... method and then reset it to the old title in the viewWillAppear method of your first VC. This will save you from having to create a global variable in your first VC or having to pass the title as a property to your second VC, although both solutions should work as well.

Bek
  • 2,206
  • 20
  • 35
  • Thanks! However, right before your comment I finally found a similar enough example on this site. However now I have a new problem..see new comments/code above. Appreciate any insight! – Renee Olson Dec 18 '14 at 00:32