【问题标题】:Swift - prepareForSegue not working [duplicate]Swift - prepareForSegue 不工作 [重复]
【发布时间】:2017-12-01 03:14:50
【问题描述】:

我有三个数组来创建一个 tableView 和一个第二个 ViewController。一个显示 tableView 上每个部分的标题,一个显示标题,一个显示每个单元格的链接。当我构建并点击其中一个单元格时,我得到:

致命错误:在展开可选值时意外发现 nil

在这一行:

 let url = URL(string: websiteURL)!

现在我相信这是因为我的 prepareForSegue 没有工作,这意味着它没有将数组数据发送到第二个视图控制器(ChannelViewController)。 我还通过添加

对此进行了测试
print("hello")

prepareForSegue 函数并查看调试器中是否出现“hello”。但它没有。

所以我猜主要问题是让 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.
}

【问题讨论】:

  • func prepare(for segue: UIStoryboardSegue, sender: Any?)替换func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?),因为你似乎使用的是Swift 3。如果它有效,那么它与此有关:stackoverflow.com/questions/40491818/…
  • 顺便说一句,强制解开url 会导致崩溃。使用条件展开并显示适当的消息

标签: ios arrays swift xcode uitableview


【解决方案1】:

您使用 prepare for segue 的方式不正确。

你应该使用

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

也可以通过

来调用它
self.performSegue(withIdentifier: "segue", sender: nil)

【讨论】:

  • 修复prepare(for:sender:) 的签名将解决此问题。将sender 设置为nil 不会有任何区别。事实上sender 应该是indexPath 然后整个与myIndex 共舞可以避免
  • 哦。同意发件人的东西。我写的是一般术语没有。我的错。
  • 没问题。这更像是对提问者的评论,而不是你自己。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多