Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew Bernard
23,262 PointsArray is not passing data in segue.
I am having an issue passing the contents of my Array to my WorkoutViewController. The array in MainViewController is populated with three workouts from a previous view. As I do the segue to the WorkoutViewController the Array prints nil. I can't find where I am dropping the data. Any help would be amazing! Thank you.
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let manager = WorkoutDataSource()
var randomWorkouts = [] as [Workout]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
randomWorkouts = manager.getRandomWorkouts()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.randomWorkouts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)-> UITableViewCell {
let workout = self.randomWorkouts[indexPath.row] as? Workout
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? WorkoutCell!
cell!.textCell?.text = workout?.title
cell!.backgroundColor = workout?.color
cell!.count.text = "\(indexPath.row+1)"
cell!.selectionStyle = UITableViewCellSelectionStyle.none
return cell!
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "detailview"){
let cell = sender as? WorkoutCell
let indexPath = tableView.indexPath(for: cell!)
let nvc = segue.destination as? UINavigationController
if let tmp = randomWorkouts[indexPath!.row] as? Workout{
let dvc = nvc?.topViewController as! DetailViewController
dvc.workout = tmp
}
}
let dvc = segue.destination as? WorkoutViewController
print("segue")
let randomWorkoutsToPass = self.randomWorkouts
print("size of array (randomWorkoutsToPass) before passing it through: ", randomWorkoutsToPass.count)
print("size of array (randomWorkouts) before passing it through: ", randomWorkouts.count)
dvc?.workouts = randomWorkoutsToPass
print("size of array after passing it through: ", dvc?.workouts.count as Any)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.tableView.contentInset = UIEdgeInsetsMake(0,0,55,0)
}
}
class WorkoutViewController: UIViewController, MZTimerLabelDelegate {
@IBOutlet weak var mainLabel: UILabel!
@IBOutlet weak var nextUp: UILabel!
@IBOutlet weak var workoutLabel: UILabel!
@IBOutlet weak var timerLabel: MZTimerLabel!
let restTime = 6.0
let workoutTime = 30.0
var index = 0
//let dataSource = MainViewController()
var workouts:[Workout]!
var randomWorkouts:[Workout]!
override func viewDidLoad() {
super.viewDidLoad()
timerLabel.delegate = self
timerLabel.timerType = MZTimerLabelTypeTimer
timerLabel.setCountDownTime(10)
timerLabel.timeFormat = "ss"
timerLabel.resetTimerAfterFinish = true
timerLabel.start()
workouts = randomWorkouts
workoutLabel.text = (workouts[0]).title
self.navigationController?.navigationBar.barTintColor = (workouts[0]).color
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
bannerView.adUnitID = "ca-app-pub-6975182968591131/5946476788"
bannerView.rootViewController = self
bannerView.load(GADRequest())
}
func timerLabel(_ timerLabel: MZTimerLabel!, finshedCountDownTimerWithTime countTime: TimeInterval) {
if index % 2 == 0 {
let wk = workouts[index / 2]
timerLabel.text = "\(workoutTime)"
mainLabel.text = (wk).title
self.view.backgroundColor = (wk).color
timerLabel.setCountDownTime(workoutTime);
workoutLabel.text = "Rest !!"
print("\(index)")
self.navigationController?.navigationBar.barTintColor = (wk).color
} else if index == workouts.count * 2 - 1 {
timerLabel.pause()
print("Paused")
dismiss(animated: true, completion: nil)
}
else {
let wk = workouts[index / 2 + 1]
timerLabel.text = "\(restTime)"
mainLabel.text = "Rest !!"
self.view.backgroundColor = (wk).color
timerLabel.setCountDownTime(restTime);
workoutLabel.text = (wk).title
print("\(index)")
self.navigationController?.navigationBar.barTintColor = (wk).color
}
index += 1;
if !((index/2) == workouts.count) {
timerLabel.start()
print("Adding to index: \(index)")
} else {
print("Workout done")
}
}
}
}