I'm out of ideas and I have no idea how to solve this. Any help will be appreciated.
I've got the following situation:
- MainViewController which points to a CollectionView embedded in a NavController
- CollectionView points to a DetailViewController
- DetailViewController shows the full picture chosen in the CollectionView and has a button which passes this image to the MainViewController. Protocol and delegate are used for this
- This image should now be shown in an UIImageView in the MainViewController
The delegation works. I have an UIImage in my MainViewController. BUT: the UIImageView is nil. So when I set the chosen Image as the imageView.image it crashes. The MainViewController is initialized and I also see my default Picture. See below for the code, and the console-output.
MainViewController
class MainViewController: UIViewController, UserChosePhoto {
@IBOutlet weak var googleImageView: UIImageView!
var usedImage: UIImage? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
googleImageView.image = usedImage
println("viewDidLoad: \(googleImageView)")
}
func userHasChosen(image: UIImage) {
usedImage = image
println("imageView: \(googleImageView)")
println("delegation: \(image)")
}
}
DetailViewController
protocol UserChosePhoto {
func userHasChosen(image: UIImage)
}
class GoogleDetailViewController: UIViewController {
@IBOutlet weak var bigImageView: UIImageView!
var image: UIImage? = nil
var delegate: UserChosePhoto? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
bigImageView.image = image
let barButtonItem = UIBarButtonItem(title: "Use", style: UIBarButtonItemStyle.Plain, target: self, action: "tapped")
self.navigationItem.rightBarButtonItem = barButtonItem
self.delegate = MainViewController()
}
func tapped() {
if (delegate != nil) {
self.delegate!.userHasChosen(bigImageView.image!)
}
self.dismissViewControllerAnimated(true, completion: nil)
}
}
Console Output: the first viewDidLoad-log comes from the app launch. found 10 pictures - a log from an API-call. imageView - this is the imageView in question. delegation: here i have an UIImage
viewDidLoad: <UIImageView: 0x7f94825cabf0; frame = (180 159; 240 282); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7f94825caeb0>> - (null)
found 10 pictures
imageView: nil
delegation: <UIImage: 0x7f94848344a0>, {213, 290}
Any tips would be cool - I'm really lost right now.
Thanks!
This is likely the issue:
self.delegate = MainViewController()
Here you are setting the
DetailViewController
's delegate to a new instance ofMainViewController
(hence the parenthesis). You need to set the delegate to its parent (which is also of typeMainViewController
)When you call
self.dismissViewControllerAnimated(true, completion: nil)
you are now going back to your originalMainViewController
but the functionuserHasChosen
was never called on this View Controller, and thus it crashes.So, essentially what you need to do is make sure the
delegate
of yourDetailViewController
is set to its parent, which you might be able to get by just doingmyDetailViewController.parent
(Sometimes this doesn't work for me, but I'm not in XCode to test at the moment) or you can pass a reference of the parent to your child view controller and set that as itsdelegate
.EDIT: I made a few assumptions on how your code and view controller's are set up that may not be entirely accurate. If your control flow indeed goes MainVC -> CollectionView -> DetailVC, then you may not be able to just get its "parent". Instead you'll have to do the second part of my suggestion which is just pass a reference of your MainVC to your DetailVC and then you can use it in that ViewController instead of creating a new
MainViewController
.And because it may be unclear: When I refer to the
DetailViewController
's parent, I mean theMainViewController
in which it came from, not the CollectionView (even though the button from there is what took you to the DetailVC)