我正在尝试将categoriesTableViewController中的数据传递给quotesContentViewController.Here是代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DestViewController : quotesContentsViewController = segue.destinationViewController as! quotesContentsViewController
let categoryString = sender?.titleLabel!!.text
let quotesContentURLString = "http://quotes.rest/qod.json?category=" + categoryString!
if let quotesContentURL = NSURL(string: quotesContentURLString) {
self.updateQuotesContentFeed(quotesContentURL, completion: {(feed) -> Void in
//print(feed!.item.imageURL)*
DestViewController.feed = feed!
DestViewController.author.text = feed!.item.author
DestViewController.quotes.text = feed!.item.quotes
DestViewController.imageURLString = feed!.item.imageURL
//print ("updateQuotesContentFeed")*
//print(DestViewController.imageURLString)*
})
}
}
DestViewController.author.text
和DestViewController.quotes.text
已成功传递到quotesContentViewController
,但DestViewController.imageURLString
失败。 print(DestViewController.imageURLString)
打印出我想要的内容。(这就是为什么我认为它应该已成功通过)
现在代码quotesContentViewController
:
import UIKit
class quotesContentsViewController: UIViewController {
var feed: quotesContentFeed?
var urlSession: NSURLSession!
weak var dataTask: NSURLSessionDataTask?
var imageURLString: String!
@IBOutlet var quotes: UILabel!
@IBOutlet var author: UILabel!
@IBOutlet var backgroundImage: UIImageView!
override func viewDidLoad() {
//let request = NSURLRequest(URL: self.imageURL!)
//dataTask = self.urlSession.dataTaskWithRequest(request) {(data, response, error) -> Void //in
//NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
//if error == nil && data != nil {
//let image = UIImage(data: data!)
//self.backgroundImage.image = image
//}
//})
//}
print("viewDidLoad")
print(self.imageURLString)
super.viewDidLoad()
// Do any additional setup after loading the view.
}
当我运行它时,它会抛出错误fatal error: unexpectedly found nil while unwrapping an Optional value
。您可以查看下面错误的屏幕截图。