0
這段代碼很簡單:代表返回nil - 斯威夫特
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
。有誰知道爲什麼?謝謝!
編輯
解決了!
你在哪裏設置了代表? – Raptor
在第一個視圖控制器「PhotoList」中。我追加的名稱和形象的陣列 –
FUNC sendDetails(名稱:字符串,照片:UIImage的){ whoTookArray.append(名稱) imageArray.append(照片) 的tableView .reloadData() !} –