1

I am trying to segue an array filled with Strings from the FactsViewController to the FavoritesViewController, but for some reason when I add some elements to the array, run the code, and transfer to the FavoritesViewController it says there are no elements in the array...

First View Controller:

import UIKit

class FactsViewController: UIViewController {

@IBOutlet var factsLabel: UILabel!
@IBOutlet var nextButton: UIButton!
@IBOutlet var previousButton: UIButton!
@IBOutlet var favoriteButton: UIButton!
var factNumber: Int = 0
var i = 0
var favoriteFactsList: [String] = []
var factsList: [String] = ["Fact 1", "Fact 2", "Fact 3", "Fact 4"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    factsLabel?.text = factsList[factNumber]
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// Segue array to Favorites ViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var destinationViewController: FavoritesViewController = segue.destinationViewController as FavoritesViewController
    var arrayToSegue: [String] = favoriteFactsList
    destinationViewController.favoriteList = arrayToSegue
}

// Next Button
@IBAction func nextButton(UIButton: AnyObject) {
    factNumber++

    if(factNumber >= factsList.count - 1) {
        factNumber = factsList.count - 1
    }

    factsLabel.text = factsList[factNumber]
}

// Previous Button
@IBAction func previousButton(UIButton: AnyObject) {
    factNumber--

    if(factNumber <= 0) {
        factNumber = 0
    }

    factsLabel.text = factsList[factNumber]
}

// Favorite Button
@IBAction func favoriteButton(UIButton: AnyObject) {
    favoriteFactsList.append("\(factsList[factNumber])")
    NSLog(String(favoriteFactsList.count))
}

// Present Favorites ViewController
@IBAction func favoritesViewController(UIButton: AnyObject) {
    let favoritesViewController = self.storyboard?.instantiateViewControllerWithIdentifier("favoritesStoryBoard") as FavoritesViewController
    self.presentViewController(favoritesViewController, animated: true, completion: nil)
}

}

Second View Controller:

import UIKit

class FavoritesViewController: FactsViewController {

@IBOutlet var favoriteFactsLabel: UILabel!
@IBOutlet var favoriteNextButton: UIButton!
@IBOutlet var favoritePreviousButton: UIButton!

var favoriteList: [String] = [String]()
var favoriteFactNumber: Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    factsList = favoriteFactsList

    NSLog("\(favoriteFactsList.count)")

    if(favoriteList.count == 0) {
        favoriteFactsLabel.text = "Sorry, You Have Not Favorited Any Facts Yet"
        favoriteNextButton.hidden = true
        favoritePreviousButton.hidden = true
    }
}

@IBAction func favoriteNextButton(UIButton: AnyObject) {
    favoriteFactNumber++

    if(favoriteFactNumber >= favoriteList.count - 1) {
        favoriteFactNumber = favoriteList.count - 1
    }

    favoriteFactsLabel.text = favoriteList[favoriteFactNumber]
}

@IBAction func favoritePreviousButton(UIButton: AnyObject) {
    favoriteFactNumber--

    if(favoriteFactNumber <= 0) {
        favoriteFactNumber = 0
    }

    favoriteFactsLabel.text = favoriteList[favoriteFactNumber]
}

override func favoriteButton(UIButton: AnyObject) {
    favoriteList.append("\(factsList[factNumber])")
}

@IBAction func returnButton(UIButton: AnyObject) {
    let factsViewController = self.storyboard?.instantiateViewControllerWithIdentifier("factsStoryBoard") as FactsViewController
    self.presentViewController(factsViewController, animated: true, completion: nil)
}

}

Why will my facts array not segue from the FactsViewController to the FavoritesViewController?

3
  • The answer to your question, is because you're not doing a segue, so prepareForSegue isn't going to be called. What you're doing is a manual presentation. Commented Apr 20, 2015 at 5:17
  • How would you preform a manual presentation? Commented Apr 20, 2015 at 5:28
  • That's what you're currently doing in your returnButton function. If you want prepareForSegue to be called, you need to perform a segue, like Viral Savaj shows in his answer. Commented Apr 20, 2015 at 5:31

1 Answer 1

3

Your error lies in the method you're using to present FavoritesViewController. You are currently presenting the ViewController via presentViewController(), not a segue, so the method will never be called. You can make a segue from FactsViewController to FavoritesViewController, with a segue identifier. You could possibly call it goToFavoritesView.

// Present Favorites ViewController
@IBAction func favoritesViewController(UIButton: AnyObject) {
    self.performSegueWithIdentifier("goToFavoritesView", sender: self)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.