代表返回nil - Swift

时间:2015-06-10 03:52:51

标签: ios core-data delegates

这段代码很简单:

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。有谁知道为什么?谢谢!

修改

解决!

1 个答案:

答案 0 :(得分:1)

添加以下代码:

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

你所有的麻烦都消失了! Tableview已成功更新!

感谢您的帮助!!