Не уверен, что я здесь делаю неправильно. Я перечислил, что моя проблема и мой код. Хотелось бы получить помощь.IBOutlet nil after instantiateViewControllerWithIdentifier
Спасибо!
Проблема:
- VC1 представляет VC2.
- IBOutlets от VC1 теперь нуль.
- VC2 отклоняется обратно в VC1.
- IBOutlets в VC1 все еще ноль.
VC1 загружает сетку изображений из flickR. VC2 имеет контроллер searchBar, когда VC2 отклоняется, он выполняет запрос flickR. Прямо перед моим self.collectionView.reload() терпит неудачу, потому что self.collectionView равна нулю :(
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)
})
}
}
Использование performSegueWithIdentifier() не даст вам никаких проблем. Не уверен, что instantiateViewControllerWithIdentifier вызывает ошибку. – b26
Откуда вы знаете, что выходы VC1 равны нулю после того, как вы представили VC2 (это не должно быть так)? Почему в методе searchBarSearchButtonClicked вы создаете новый экземпляр MainViewController? Вы не должны этого делать. Стандартный способ вызова метода в контроллере, к которому вы возвращаетесь, - это делегирование. – rdelmar
Спасибо за ответ :) В функции VC2s viewDidLoad() я запускаю этот бит кода println (MainViewController(). ControllerView), чтобы узнать, существует ли он, но он всегда возвращает nil. Я попробую метод делегирования и обновить этот пост. – b26