我整天都被困在这上面了。
我的代表团设置得当。我有VC 1,我可以点击右上角的栏按钮项目打开VC 2。
在VC中,我可以添加一个人的姓名和照片,并将信息发送回VC 1,在那里将对象放入一系列collectionView单元格中。
然后,我设置了DidSelectItemAtIndexPath方法,所以当我点击collectionViewCell时。它会分段到相同的“+”屏幕(VC 2)
此时它只将标题更改为“删除人员”,并以编程方式将“废纸篓”按钮添加到左侧的导航栏。
当我点击“垃圾箱”按钮时,它会打印到日志“人员已删除”。 在这里遇到问题
如果我能够添加和编辑人物图像和名称并永久保存,我的协议和代表显然是正确的。
我不知道如何将deleteItemAtIndexPath函数添加到我添加的垃圾桶按钮中。
这适用于斯威夫特,如果有人能给我我所缺少的东西,我会非常感激。一切正常,但我无法弄清楚如何构建垃圾按钮,以便删除所选项目。
谢谢!
import UIKit
protocol AddNewTrainerViewControllerDelegate: class
{
func addNewTrainerViewControllerDidCancel(controller: AddNewTrainerViewController)
func addNewTrainerViewController(controller: AddNewTrainerViewController, didFinishAddingItem item: TrainerArray)
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
func addNewTrainerViewController(controller: AddNewTrainerViewController, didFinishEditingItem trainer: TrainerArray)
}
class AddNewTrainerViewController: UITableViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBAction func openPhotoLibrary(sender: AnyObject) {
var photoPicker = UIImagePickerController()
photoPicker.delegate = self
photoPicker.sourceType = .PhotoLibrary
photoPicker.allowsEditing = true
self.presentViewController(photoPicker, animated: true, completion: nil)
}
@IBOutlet weak var trainerPhoto: UIImageView!
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
trainerPhoto.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(false, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
if let trainer = trainerToEdit {
title = "Edit Trainer"
textField.text = trainer.name
trainerPhoto.image = trainer.trainerImage
doneBarButton.enabled = true
let deleteButton = UIBarButtonItem(barButtonSystemItem: .Trash, target: self, action: "deleteButton")
navigationItem.leftBarButtonItem = deleteButton
}
}
func deleteButton() {
print("Trainer Deleted")
dismissViewControllerAnimated(true, completion: nil)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
textField.becomeFirstResponder()
}
//outlet for done bar button item to control status
@IBOutlet weak var doneBarButton: UIBarButtonItem!
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let oldText: NSString = textField.text!
let newText: NSString = oldText.stringByReplacingCharactersInRange(range, withString: string)
if newText.length > 3 {
doneBarButton.enabled = true
} else {
doneBarButton.enabled = false
}
return true
}
//Trainer Name Text Field
@IBOutlet weak var textField: UITextField!
//action outlet for done button
@IBAction func doneButton(sender: UIBarButtonItem) {
if let trainer = trainerToEdit {
trainer.name = textField.text!
trainer.trainerImage = trainerPhoto.image!
delegate?.addNewTrainerViewController(self, didFinishEditingItem: trainer)
} else if trainerPhoto.image == nil {
displayAlert("Please Add Your Trainer's Photo", message: "")
} else if trainerPhoto.image != nil {
let item = TrainerArray()
item.name = textField.text!
item.trainerImage = trainerPhoto.image!
delegate?.addNewTrainerViewController(self, didFinishAddingItem: item)
}
}
//action outlet for cancel or back button
@IBAction func cancelButton(sender: UIBarButtonItem) {
delegate?.addNewTrainerViewControllerDidCancel(self)
}
// doesn't allow selection of the row. Won't turn gray.
override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
return nil
}
weak var delegate: AddNewTrainerViewControllerDelegate?
func displayAlert (title: String, message: String) {
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
var trainerToEdit: TrainerArray?
}
这是VC 2中的代码,其中删除按钮是。