传递时在控制台中显示nil结果的输出

时间:2017-12-06 10:57:44

标签: ios swift4 xcode9.1

我是新的iOS开发并且还在学习基础知识,我的查询是当我从我的主tableView控制器将数据传递给其他视图控制器以便它顺利通过但输出结果在控制台中显示为nil,如下所示 here as you can see output in console is nil.This is my second view controller named as movieDetailViewController

下面是我的主tableView控制器,我将调用第二个视图控制器。

   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? movieDetailViewController
    {
        destination.film = movies[(tables.indexPathForSelectedRow?.row)!] as? Movie
    }
  }
}

这里的结构Movie是一个普通的swift文件,包含我要打印的属性,如下所示:

import Foundation

struct Movie {

     var title: String
     var overview: String
     var poster_path: String

}

现在我描述传递到另一个视图控制器的输出显示为nil。我从1周开始就遇到了这个问题并且搜索了很多,但没有任何解决方案可以帮助我。

3 个答案:

答案 0 :(得分:0)

试试这个:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetails", let destination = segue.destination as? movieDetailViewController {
        if let cell = sender as? UITableViewCell, let indexPath = tables.indexPath(for: cell) {
            destination.film = movies[indexPath.row] as? Movie
        }
    }
}

答案 1 :(得分:0)

试试这个

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


 let mv = movies[indexPath.row]

 performSegue(withIdentifier: "showDetails", sender: mv)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if let destination = segue.destination as? movieDetailViewController
{
    destination.film = sender as? Movie
}
}

答案 2 :(得分:0)

在movieTable View Controller类中替换此代码

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? movieDetailViewController
    {
        if let selectedIndex = tables.indexPathForSelectedRow?.row,
            let selectedTitle = movies[selectedIndex]["title"],
            let selectedOverView = movies[selectedIndex]["overview"],
            let selectedPosterPath : String = movies[selectedIndex]["poster_path"] as! String
            {
                destination.film = Movie.init(title: selectedTitle as! String, overView: selectedOverView as! String, posterPath:selectedPosterPath) as! Movie
            }
    }
}

为结构添加初始化

init(title t: String, overView: String, posterPath pp: String) {
    title = t
    overview = overView
    poster_path = pp
}

viewDidLoad中的movieDetailsViewController代码替换为

override func viewDidLoad() {
        super.viewDidLoad()
        if let title = film?.title,
           let movieoverview = film?.overview,
            let urlString = film?.poster_path
        {
            movieTitle.text = title
            print(movieTitle.text)

            movieOverview.text = movieoverview

            print(movieOverview.text)

            let urlString = URL(string: ("http://image.tmdb.org/t/p/w185/") + urlString)
            moviePoster.downloadedFrom(url: urlString!)

        }


    }

希望这会有所帮助。快乐的编码。