gpt4 book ai didi

ios - 此类对于键取消不符合键值编码

转载 作者:IT王子 更新时间:2023-10-29 05:14:56 24 4
gpt4 key购买 nike

我不断收到此错误:Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FoodTracker.MealViewController 0x7faa9ed189d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key cancel.'

我正在尝试完成 Apple 开发人员指南以开始使用 iOS 应用程序。我的代码和 Storyboard 看起来与示例文件中的代码和 Storyboard 完全一样。我希望新人能够看到我不是的东西?

import UIKit
import os.log

class MealViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

//MARK: Properties
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var photoImageView: UIImageView!
@IBOutlet weak var ratingControl: RatingControl!
@IBOutlet weak var saveButton: UIBarButtonItem!
/*
This value is either passed by 'MealTableViewController' in
'prepare(for:sender) or constructed as part of adding a new meal.
*/
var meal: Meal?

override func viewDidLoad() {
super.viewDidLoad()

// Handle the text field's user input through delegate callbacks
nameTextField.delegate = self

// Enable save button only if text field has valid Meal name
updateSaveButtonState()
}

//MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Hide the keyboard
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
updateSaveButtonState()
navigationItem.title = textField.text
}
func textFieldDidBeginEditing(_ textField: UITextField) {
// Disable save button while editing
saveButton.isEnabled = false
}

//MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// Dismiss the picker if the user canceled
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image
photoImageView.image = selectedImage
// Dismiss the picker
dismiss(animated: true, completion: nil)
}

//MARK: Navigation
@IBAction func cancel(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}


// Configure view controller before it's presented
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)

// Configure destination view controller only when save button pressed
guard let button = sender as? UIBarButtonItem, button === saveButton else {
os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
return
}

let name = nameTextField.text ?? ""
let photo = photoImageView.image
let rating = ratingControl.rating

// Set meal to be passed to MealTableViewController after unwind segue
meal = Meal(name: name, photo: photo, rating: rating)
}

//MARK: Actions
@IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
// Hide the keyboard
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken
imagePickerController.sourceType = .photoLibrary
// Make sure ViewController is notified when the user picks an image
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}

//MARK: Private Methods
private func updateSaveButtonState() {
// Disable the save button if the text field is empty
let text = nameTextField.text ?? ""
saveButton.isEnabled = !text.isEmpty
}
}

还有一些其他文件,但请告诉我您需要什么,因为我是 Swift/XCode 的新手,不确定该提供/不提供什么。

最佳答案

这意味着您的 Storyboard上有一些东西连接到 IBOutlet 称为 cancel 但您的类中没有这个 IBOutlet。因此编译器无法在您的类中找到 Key cancel(它表示属性)。你应该在 Storyboard 中找到那个按钮(我认为它是一个 UIButton 因为名字),在它上面单击鼠标右键并单击“x”删除该连接。或者您可能想要完全删除此按钮。或者您可能想将此 IBOutlet 添加到您的类(class)中。

关于ios - 此类对于键取消不符合键值编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45902651/

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