将数据从一个视图控制器传递到另一个时出错
Error when passing data from one view controller to the other
我目前正在 swift 2 中制作 rss 提要 reader,但我很难尝试将 url 从我的表格视图传递到我的 Web 视图。当我在 tableviewcontroller 中打印 url 时,它会在 url 前面添加(可选)。但是,当它传递到我的 webviewcontroller 时,它前面没有(可选)inf。这让我担心途中出了点问题。
tableviewcontroller 将 url 传递给 webviewcontroller:
override func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath){
let webVC = WebViewController()
webVC.entryUrl = entriesArray[indexPath.row]["link"]
self.navigationController?.pushViewController(webVC, animated: true)
print(entriesArray[indexPath.row]["link"])
}
webviewcontroller 接收数据并尝试使用它:
var entryUrl:String!
override func viewDidLoad() {
super.viewDidLoad()
print(entryUrl)
if entryUrl != nil {
print(entryUrl)
let requesturl: NSURL? = NSURL(string: entryUrl)
let request = NSURLRequest(URL: requesturl!)
web.loadRequest(request)
}
loadAddressURL()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func loadAddressURL() {
if let requestURL = NSURL(string: entryUrl) {
let request = NSURLRequest(URL: requestURL)
web?.loadRequest(request)
}
}
我在线上遇到错误:
loadAddressURL()
说:
EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose
从 tableviewcontroller 日志打印:
可选(“http://www.google.com/”)
但是从 webviewcontroller 日志只打印:
http://www.google.com/
之后:
致命错误:在展开可选值
时意外发现 nil
为什么这行不通,为什么 url 在 webviewcontroller 中不再是可选的?
更改代码行以声明视图控制器的对象,如下所示。
override func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath){
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let webVC = mainStoryboard.instantiateViewControllerWithIdentifier("WebViewController") as! WebViewController
webVC.entryUrl = entriesArray[indexPath.row]["link"]
self.navigationController?.pushViewController(webVC, animated: true)
print(entriesArray[indexPath.row]["link"])
}
别忘了在故事板中添加 viewController 的标识符。
我目前正在 swift 2 中制作 rss 提要 reader,但我很难尝试将 url 从我的表格视图传递到我的 Web 视图。当我在 tableviewcontroller 中打印 url 时,它会在 url 前面添加(可选)。但是,当它传递到我的 webviewcontroller 时,它前面没有(可选)inf。这让我担心途中出了点问题。
tableviewcontroller 将 url 传递给 webviewcontroller:
override func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath){
let webVC = WebViewController()
webVC.entryUrl = entriesArray[indexPath.row]["link"]
self.navigationController?.pushViewController(webVC, animated: true)
print(entriesArray[indexPath.row]["link"])
}
webviewcontroller 接收数据并尝试使用它:
var entryUrl:String!
override func viewDidLoad() {
super.viewDidLoad()
print(entryUrl)
if entryUrl != nil {
print(entryUrl)
let requesturl: NSURL? = NSURL(string: entryUrl)
let request = NSURLRequest(URL: requesturl!)
web.loadRequest(request)
}
loadAddressURL()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func loadAddressURL() {
if let requestURL = NSURL(string: entryUrl) {
let request = NSURLRequest(URL: requestURL)
web?.loadRequest(request)
}
}
我在线上遇到错误:
loadAddressURL()
说:
EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose
从 tableviewcontroller 日志打印: 可选(“http://www.google.com/”)
但是从 webviewcontroller 日志只打印: http://www.google.com/
之后: 致命错误:在展开可选值
时意外发现 nil为什么这行不通,为什么 url 在 webviewcontroller 中不再是可选的?
更改代码行以声明视图控制器的对象,如下所示。
override func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath){
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let webVC = mainStoryboard.instantiateViewControllerWithIdentifier("WebViewController") as! WebViewController
webVC.entryUrl = entriesArray[indexPath.row]["link"]
self.navigationController?.pushViewController(webVC, animated: true)
print(entriesArray[indexPath.row]["link"])
}
别忘了在故事板中添加 viewController 的标识符。