gpt4 book ai didi

ios - 委托(delegate)返回 nil - Swift

转载 作者:行者123 更新时间:2023-11-29 01:56:51 24 4
gpt4 key购买 nike

这段代码很简单:

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

我将文本和图像委托(delegate)给 PhotoList View Controller ,但它在此处的委托(delegate)行中崩溃:

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

你所有的烦恼都消失了!表格 View 更新成功!

感谢您的帮助!!

关于ios - 委托(delegate)返回 nil - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30746791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com