先用storyboard見好 segue, 取名 goWebview
接下來實作prepare
// 選到我們要的segue
if segue.identifier == "goWebview" {
}
// 把目的地轉成他的類別
let viewController = segue.destination as! ViewController
// 得到我們看的table view index
let index = tableView.indexPathForSelectedRow
// assign 數值
viewController.movie = movieSelected
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goWebview" {
let viewController = segue.destination as! ViewController
let index = tableView.indexPathForSelectedRow
let movieSelected = movies[index!.row]
//print(index?.row)
viewController.movie = movieSelected
}
}
要先在 ViewController 裡面新增 var movie: Movie?
class ViewController: UIViewController {
var movie: Movie?
override func viewDidLoad() {
super.viewDidLoad()
print("你選擇的電影是\(movie!.title)")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
沒有留言:
張貼留言