没想到发现没有。将 url 从一个控制器传递到另一个控制器时
Unexpectedly found nil. While passing url from one controller to another
我的项目是创建一个拆分视图,其中 master 是 TableViewController,Detail 是 WebKit。我的主要问题是,当我从我的 TableView 传递一个 Url 时,它在 Webkit Controller 中展开它时变成了山丘。我还使用带有变量 name:Strin 的结构列表?和 url: URL!
这是我的表格代码ViewController:
import UIKit
import WebKit
class WebBrowsers: UITableViewController {
private var list:[List] = [
List(name: "google.com", url: URL(string:"google.com"))
]
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return list.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
cell.textLabel?.text = list[indexPath.row].name
// Configure the cell...
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail"{
if let destination = segue.destination as? infoBrowser{
if let row = tableView.indexPathForSelectedRow?.row{
destination.detailUrl = list[row].url
}
}
}
}
}
这是我的 webkit ViewController
import UIKit
import WebKit
class infoBrowser: UIViewController {
var detailUrl: URL!
@IBOutlet weak var showPage: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: detailUrl!)
showPage.load(request)
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
}
}
这行代码有问题:
if let row = tableView.indexPathForSelectedRow?.row{
destination.detailUrl = list[row].url
}
tableView.indexPathForSelectedRow?将始终为 nil,因为没有实现 tableView 的 didSelectRow 委托。
将您的导航代码转移到 tableView 的 didSelect 或仅将其实现到 select 行。
URL(string:"google.com")
这个 return 是一个可选的 URL 这意味着,如果传递的字符串不是有效的 url 那么它将 return nil
并且在您的 infoBrowser
中您已将 detailUrl
声明为 implicit optional
这意味着当它在创建 URL 请求时尝试访问此变量时,如果值为 nil 它将崩溃,
所以在创建 URL 请求之前更改 var detailUrl: URL?
添加检查以确保 URL 不是 nil
override func viewDidLoad() {
super.viewDidLoad()
guard let url = detailUrl else { return }
let request = URLRequest(url: url)
showPage.load(request)
}
我的项目是创建一个拆分视图,其中 master 是 TableViewController,Detail 是 WebKit。我的主要问题是,当我从我的 TableView 传递一个 Url 时,它在 Webkit Controller 中展开它时变成了山丘。我还使用带有变量 name:Strin 的结构列表?和 url: URL!
这是我的表格代码ViewController:
import UIKit
import WebKit
class WebBrowsers: UITableViewController {
private var list:[List] = [
List(name: "google.com", url: URL(string:"google.com"))
]
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return list.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
cell.textLabel?.text = list[indexPath.row].name
// Configure the cell...
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail"{
if let destination = segue.destination as? infoBrowser{
if let row = tableView.indexPathForSelectedRow?.row{
destination.detailUrl = list[row].url
}
}
}
}
}
这是我的 webkit ViewController
import UIKit
import WebKit
class infoBrowser: UIViewController {
var detailUrl: URL!
@IBOutlet weak var showPage: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: detailUrl!)
showPage.load(request)
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
}
}
这行代码有问题:
if let row = tableView.indexPathForSelectedRow?.row{
destination.detailUrl = list[row].url
}
tableView.indexPathForSelectedRow?将始终为 nil,因为没有实现 tableView 的 didSelectRow 委托。 将您的导航代码转移到 tableView 的 didSelect 或仅将其实现到 select 行。
URL(string:"google.com")
这个 return 是一个可选的 URL 这意味着,如果传递的字符串不是有效的 url 那么它将 return nil
并且在您的 infoBrowser
中您已将 detailUrl
声明为 implicit optional
这意味着当它在创建 URL 请求时尝试访问此变量时,如果值为 nil 它将崩溃,
所以在创建 URL 请求之前更改 var detailUrl: URL?
添加检查以确保 URL 不是 nil
override func viewDidLoad() {
super.viewDidLoad()
guard let url = detailUrl else { return }
let request = URLRequest(url: url)
showPage.load(request)
}