委托返回 nil - Swift

Delegate returning nil - Swift

这段代码很简单:

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

    }

}

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)
        }
    }
}

我正在将文本和图像委托给 PhotoList 视图控制器,但它在此处的委托行中崩溃:

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

打印行表示它是 nil。有谁知道为什么?谢谢!

编辑

已解决!

添加此代码:

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

你所有的麻烦都没有了!表格视图更新成功!

感谢帮助!!