2017-06-27 66 views
1

我有三個數組來製作tableView和第二個ViewController。一個顯示tableView中每個部分的標題,一個顯示標題,一個顯示每個單元格的鏈接。當我建立並點擊其中的一個單元,我得到:Swift - prepareForSegue不工作

fatal error: unexpectedly found nil while unwrapping an Optional value

在這條線:

let url = URL(string: websiteURL)! 

現在,我相信這是因爲我的prepareForSegue不能正常工作,這意味着它不發送將數組數據傳遞給第二個View Controller(ChannelViewController)。 我還測試了這一點,添加

print("hello") 

prepareForSegue功能,看是否「你好」出現在調試器。但事實並非如此。

所以我猜主要問題是使prepareForSegue做它應該做的。

這裏是FirstViewController代碼:

import UIKit 




class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 



var headers = ["Bein Sports", "Sky Sports", "Alkass", "Other"] 

var channels = [["Bein Sports 1","Bein Sports 2","Bein Sports 3","Bein Sports 4","Bein Sports 5","Bein Sports 6","Bein Sports 7","Bein Sports 8","Bein Sports 9","Bein Sports 10","Bein Sports News"], 
          ["Sky Sports 1","Sky Sports 2","Sky Sports 3","Sky Sports 4","Sky Sports 5"], 
          ["Alkass One", "Alkass Two", "Alkass Three", "Alkass Four", "Alkass Five"], 
          ["BT Sports 1", "BT Sports 2", "Real Madrid TV", "Real Madrid TV 2"]] 

var links = [["https://www.google.ca","https://www.facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"], 
      ["https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"], 
      ["https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"], 
      ["http://twitter.com","http://facebook.com","http://www.google.com","http://www.instagram.com"]] 

var myIndex : IndexPath = IndexPath() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return channels[section].count 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return channels.count //Rows 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return headers[section] //Sections 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
    cell.textLabel?.text = channels[indexPath.section][indexPath.row] //TextLabel 
    return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    myIndex = IndexPath(row: indexPath.section, section: indexPath.row) 
    performSegue(withIdentifier: "segue", sender: self) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
{ 
    if (segue.identifier == "segue") { 
     let viewController = segue.destination as! ChannelViewController 
     // Now you have a pointer to the child view controller. 
     // You can save the reference to it, or pass data to it. 
     viewController.websiteURL = links[myIndex.section][myIndex.row] 
     print("google") 
    } 
} 
} 

這裏是第二個視圖控制器代碼:

import UIKit 
import WebKit 
import AVKit 
import AVFoundation 

class ChannelViewController: UIViewController { 


    var webView: WKWebView! 
    var websiteURL = "" 



override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 



    webView = WKWebView() 
    webView.scrollView.isScrollEnabled = false 


    view.addSubview(webView) 
    let url = URL(string: websiteURL)! 
    let req:URLRequest = URLRequest(url: url) 
    webView.load(req) 

    webView.translatesAutoresizingMaskIntoConstraints = false 
    let width = NSLayoutConstraint(item: webView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1.0, constant: 0) //Constraints 

    let height = NSLayoutConstraint(item: webView, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1.0, constant: 0) 
    view.addConstraints([width,height]) //Constraints 

} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
+0

更換'FUNC prepareForSegue調用它(SEGUE:UIStoryboardSegue,發件人:AnyObject)'和'FUNC準備(對於SEGUE:UIStoryboardSegue,發件人:任何?)'因爲你似乎使用Swift 3.如果它的工作,那麼它與此有關:https://stackoverflow.com/questions/40491818/didselectrowatindexpath-not-working-swift-3 – Larme

+0

作爲一邊,武力解開'url'是要求崩潰。使用條件解包並顯示適當的消息 – Paulw11

回答

3

您正在使用賽格瑞準備的方法是不正確的。

您應該使用

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

,並通過

self.performSegue(withIdentifier: "segue", sender: nil) 
+0

修復'prepare(for:sender:)'的簽名將解決問題。將'sender'設置爲'nil'不會有什麼區別。實際上'sender'可能應該是'indexPath',然後用'myIndex'跳舞的整個舞蹈可以避免 – Paulw11

+0

哦。同意發件人的東西。我把它寫成一般的術語沒有。我的錯。 – Behrad3d

+0

沒問題。這是對提問者的評論,而不是你自己。 – Paulw11