我想从我的viewcontroller调用一个数组,然后从包含对象的数组中设置文本。但是,当我执行segue它只给了我一个“索引超出范围”我不明白我做错了什么。我也尝试过使用自我。在召集的任何地方,但它似乎是问题。为什么这是索引不足?
变量电影是我试图从数组中获得1个电影,但同样的问题仍然存在。我的视图控制器做错了什么,或者我需要传递一个值?
Detailsviewcontroller是新视图和tableview的控制器,这就是为什么我在文件内部创建了另一个类。
文件: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!
}
考虑到'ViewController()'的两个实例彼此不同,并且与故事板或xib中的'ViewController'实例不同。那就是问题所在。您需要实际参考视图控制器。 – vadian
什么是行'var mainController = ViewController()'for?它看起来没有用,什么是'ViewController'类? 'var movie = ViewController()。self.movieArray [0]''试图做什么?我想这是导致问题的线路,但要清楚,请指出哪一行导致异常,并将* full *错误消息添加到您的问题。 –
[你如何在视图控制器和Swift中的其他对象之间共享数据?](http://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-controllers-and -other-objects-in-swift) – brimstone