-5

i am trying to call an array from my viewcontroller and then setting text from that array which contains objects. But when i perform the segue it only gives me an "Index out of range" I do not see what i have done wrong. I have also tried to use the self. everywhere in the call but it dosent seem to be the problem.

The variable movie is where i try to get only 1 movie from the array but the same problem persists. Have I done something wrong with my viewcontroller or do i need to pass a value in?

Detailsviewcontroller is the controller for the new view and the tableview, that is why i have created another class inside the file.

File: DetailsviewController

import UIKit
class DetailsViewController: UIViewController, UITableViewDataSource,UITabBarDelegate { 

    @IBOutlet weak var tableView: UITableView!



    var mainController = ViewController()

    var movie = ViewController().self.movieArray[0]





    var titles = ["Title", "Release Date","Genres","OverView"]



    override func viewDidLoad() {
        super.viewDidLoad()
        print(movie)
        // Do any additional setup after loading the view.
    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DetailsCustomcell

        cell.detailTitle.text = titles[indexPath.row]
        if indexPath.row == 0{
        cell.detailContent.text = ""
        }
        if indexPath.row == 1{
            cell.detailContent.text = ""
        }
        if indexPath.row == 2{
            cell.detailContent.text = "Movie genre´s is not fixed yet"
        }
        if indexPath.row == 3{
            cell.detailContent.text = ""
        }
            return cell
    }





    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
class DetailsCustomcell: UITableViewCell{

    @IBOutlet weak var detailTitle: UILabel!

    @IBOutlet weak var detailContent: UILabel!

}
vadian
  • 274,689
  • 30
  • 353
  • 361
Pr0tonion
  • 187
  • 2
  • 12
  • Consider that the two instances of `ViewController()` are different from each other and different from the `ViewController` instance in the storyboard or xib. That's the problem. You need the actual reference to the view controller. – vadian Feb 26 '17 at 21:31
  • What is the line `var mainController = ViewController()` for? It appears unused and what is the `ViewController` class? What does `var movie = ViewController().self.movieArray[0]` attempt to do? I guess this is the line causing the problem but to be clear please indicate which line is causing the exception and add the *full* error message to your question. – Robotic Cat Feb 26 '17 at 21:31
  • Possible duplicate of [How do you share data between view controllers and other objects in Swift?](http://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-controllers-and-other-objects-in-swift) – brimstone Feb 26 '17 at 23:00

1 Answers1

0

Not sure what this is:

var mainController = ViewController()
var movie = ViewController().self.movieArray[0]

Regardless, ViewController() instantiates a new object of that class. At that point, it's very likely movieArray has never been initialized and has no objects in it.

Frankie
  • 11,508
  • 5
  • 53
  • 60
  • Aha, didnt think of that! How do i specifically reference a viewcontroller then? – Pr0tonion Feb 26 '17 at 21:49
  • It really depends. But my questioning would start with why do you need to get the 'movie' from this view controller's property instead of whatever data source populated the `movieArray` to begin with? – Frankie Feb 26 '17 at 22:08
  • The reason as to why I need the movie is because i want to read the contents of the Array. The "movie" variable was just something i was testing out. I have a movieArray in my viewcontroller.swift where i do a get request then populate my movieArray with movieobjects. when the movie has been clicked on I wish to display the details of that specific movie. – Pr0tonion Feb 26 '17 at 22:15
  • 1
    Do a search for ways to pass data from master to detail view. You'll get more tutorials and answers then you'll know what to do with. Example: http://stackoverflow.com/questions/28430663/send-data-from-tableview-to-detailview-swift – Frankie Feb 26 '17 at 22:31
  • Thank you so much! – Pr0tonion Feb 26 '17 at 22:31