本文介绍了IBOutlet在instantiateViewControllerWithIdentifier之后为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不确定我在这里做错了什么。我列出了我的问题和我的代码。很想得到一些帮助。
Not sure exactly what I'm doing wrong here. I've listed out what my problem is and my code. Would love to get some help.
谢谢!
问题:
- VC1呈现VC2。
- 来自VC1的IBOutlets现在为零。
- VC2解散回VC1。
- VC1中的IBOutlets仍为零。
- VC1 presents VC2.
- IBOutlets from VC1 are now nil.
- VC2 dismisses back into VC1.
- IBOutlets in VC1 are still nil.
VC1从flickR加载图片网格。 VC2有一个searchBar控制器,当VC2被解雇时它会运行flickR查询。就在我的self.collectionView.reload()失败之前,因为self.collectionView是nil :(
VC1 loads a grid of pictures from flickR. VC2 has a searchBar controller, when VC2 is dismissed it does runs flickR query. Right before my self.collectionView.reload() fails because self.collectionView is nil :(
VC1
import UIKit
class MainViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var searchMask: UIView!
let api_key = "f87357398a52b80e0c9f76bf94c8ee94"
var flickResults:NSMutableArray! = NSMutableArray()
var titleResults:NSMutableArray! = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
loadPhotos("Brasil")
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
func loadPhotos(searchString: String) {
let flickr:FlickrHelper = FlickrHelper()
println("Collection: \(self.collectionView)")
flickr.request(searchString, per_page: 50, completion: { (searchStr, flickrPhotos, photoTitle) -> () in
dispatch_async(dispatch_get_main_queue(), {
self.flickResults = flickrPhotos
self.titleResults = photoTitle
self.collectionView.reloadData()
})
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: PhotoCell = self.collectionView.dequeueReusableCellWithReuseIdentifier("photoCell", forIndexPath: indexPath) as PhotoCell
cell.photoImageView.alpha = 0
let queue:dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue, {
let searchURL: String = self.flickResults.objectAtIndex(indexPath.item) as String
let title: String = self.titleResults.objectAtIndex(indexPath.item) as String
let image: UIImage = self.loadImage(searchURL)
dispatch_async(dispatch_get_main_queue(), {
UIView.animateWithDuration(0.5, animations: {
cell.photoImageView.image = image
cell.photoTitle.text = title.capitalizedString
cell.photoImageView.alpha = 1
})
})
})
insertBlurView(cell.titleMask, UIBlurEffectStyle.Dark)
return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.flickResults.count
}
func loadImage(imageUrl: String) -> UIImage{
let url = NSURL(string: imageUrl)
let data = NSData(contentsOfURL: url!)
let image = UIImage(data: data!)
return image!
}
@IBAction func searchButtonPressed(sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let vc: SearchViewController = storyboard.instantiateViewControllerWithIdentifier("Search") as SearchViewController
providesPresentationContextTransitionStyle = true
definesPresentationContext = true
vc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
self.presentViewController(vc, animated: false, completion: nil)
}
}
VC2
import UIKit
class SearchViewController: UIViewController, UITextFieldDelegate, UISearchBarDelegate, UICollectionViewDelegate {
@IBOutlet var searchMask: UIView!
@IBOutlet var searchField: UITextField!
@IBOutlet var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
insertBlurView(self.searchMask, UIBlurEffectStyle.Dark)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func exitTap(sender: UIButton) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
self.dismissViewControllerAnimated(true, completion: {
let mainvc = self.storyboard?.instantiateViewControllerWithIdentifier("Main") as MainViewController
mainvc.loadPhotos(searchBar.text)
})
}
}
推荐答案
感谢rdelmar,我能够使用协议来解决这个问题。
Thanks to rdelmar, I was able to figure this out using protocols.
这是我添加的内容:
- SearchDelegate
- SearchDelegate函数runSearch()
VC1
class MainViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, SearchDelegate {
func runSearch(searchString: String) {
loadPhotos(searchString)
}
}
VC2
protocol SearchDelegate {
func runSearch(searchString: String)
}
class SearchViewController: UIViewController, UITextFieldDelegate, UISearchBarDelegate, UICollectionViewDelegate {
@IBOutlet var searchMask: UIView!
@IBOutlet var searchField: UITextField!
@IBOutlet var searchBar: UISearchBar!
var delegate: SearchDelegate?
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
delegate?.runSearch(searchBar.text)
self.dismissViewControllerAnimated(true, completion:nil)
}
这篇关于IBOutlet在instantiateViewControllerWithIdentifier之后为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!