Delegate returning nil - Swift

1.7k views Asked by At

This code is simple:

import UIKit
import CoreData

class PhotoList: UIViewController, UITableViewDelegate, UITableViewDataSource, sendDetailsDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    @IBOutlet var tableView: UITableView!
    var whoTookArray: [String] = []
    var imageArray: [UIImage] = []

    func sendDetails (name: String, photo: UIImage) {
        whoTookArray.append(name)
        imageArray.append(photo)
        tableView!.reloadData()
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return whoTookArray.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
        let row = indexPath.row
        let whoTookName = whoTookArray[row]
        let image = imageArray[row]
        cell.textLabel?.text = whoTookName
        cell.imageView!.image = image
        return cell

    }

}

and

import UIKit
import CoreData

protocol sendDetailsDelegate {
    func sendDetails(name: String, photo: UIImage)
}

class Details: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "chooseImage:")
        tapGestureRecognizer.numberOfTapsRequired = 1
        imageSelected.addGestureRecognizer(tapGestureRecognizer)
        imageSelected.userInteractionEnabled = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBOutlet var whoTookTextField: UITextField!
    @IBOutlet var imageSelected: UIImageView!
    var delegate: sendDetailsDelegate?

    //Pick the image by tapping, accessing the PhotoLibrary
    func chooseImage(recognizer: UITapGestureRecognizer) {
        let imagePicker: UIImagePickerController = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }

    //Put the selected image into the screen
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

        let pickedImage: UIImage = (info as NSDictionary).objectForKey(UIImagePickerControllerOriginalImage) as! UIImage
        let smallPicture = scaleImageWith(pickedImage, newSize: CGSizeMake(288,148))
        var sizeOfImageView: CGRect = imageSelected.frame
        sizeOfImageView.size = smallPicture.size
        imageSelected.frame = sizeOfImageView
        imageSelected.image = smallPicture
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

    func scaleImageWith(image: UIImage, newSize: CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
        image.drawInRect(CGRectMake(0,0, newSize.width, newSize.height))
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }


    @IBAction func addButton(sender: AnyObject) {

        if whoTookTextField == nil || imageSelected == nil { return }
        if imageSelected == nil { return }


        let whoTook = whoTookTextField.text
        let image = imageSelected.image

        println("\(delegate)")

        delegate!.sendDetails(whoTook, photo: image!)


        println("whoTook: \(whoTook) image: \(image)")

        if let navigation = self.navigationController {
            navigation.popViewControllerAnimated(true)
        }
    }
}

I'm delegating a text and an image to the PhotoList view controller, but it crashes in the delegate line here:

delegate!.sendDetails(whoTook, photo: image!)

The print line says it's nil. Does anyone know why? Thanks!

EDIT

Solved!

1

There are 1 answers

0
Nicholas Piccoli On BEST ANSWER

add this code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "toPhotoList") {
            let view = segue.destinationViewController as! Details
            view.delegate = self
        }
    }

And all your trouble is gone! Tableview updated successfully!

Thanks for the help!!