IBAction没有在快速Xcode中注册触摸

时间:2019-01-07 18:55:26

标签: swift

我正在尝试设置一个保存按钮,然后选择返回主屏幕。问题是,按下保存按钮后,应用程序将关闭,并显示以下语句:“未按下保存按钮,正在取消”。这将从设置为在未按下按钮时激活的else语句执行。但是,它仅在按下按钮后才激活,因此我对它的工作方式以及如何修复按钮以在按下按钮时实际保存数据感到困惑。

我尝试设置一些打印语句以获取有关失败原因的一些状态更新,并尝试了不同的方法来验证按钮的按下状态。

import UIKit
import os.log

class EventViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    var saveButtonPressed: Bool = false;
    //MARK: Properties
    @IBOutlet weak var navItem: UINavigationItem!
    @IBOutlet weak var titleTextField: UITextField!
    @IBOutlet weak var titleNameLabel: UILabel!
    @IBOutlet weak var photoImageView: UIImageView!
    @IBOutlet weak var locationTextField: UITextField!
    @IBOutlet weak var locationNameLabel: UILabel!
    @IBOutlet weak var descriptionTextField: UITextField!
    @IBOutlet weak var descriptionNameLabel: UILabel!
    @IBAction func saveButton(_ sender: Any) {
        saveButtonPressed = true
        print("pressed")
    }


    /*
     This value is either passed by `EventTableViewController` in `prepare(for:sender:)`
     or constructed as part of adding a new event.
     */
    var event: Event?


    override func viewDidLoad() {
        super.viewDidLoad()
        // Handle the text field’s user input through delegate callbacks.
        navItem.title = "New Event"
        titleTextField.delegate = self
        locationTextField.delegate = self
        descriptionTextField.delegate = self

    }
//MARK: UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // Hide the keyboard.
        textField.resignFirstResponder()
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
    }
//MARK: UIImagePickerControllerDelegate
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        // Dismiss the picker if the user canceled.
        dismiss(animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        // The info dictionary may contain multiple representations of the image. You want to use the original.
        guard let selectedImage = info[.originalImage] 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

    // This method lets you configure a view controller before it's presented.
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        super.prepare(for: segue, sender: sender)
        // Configure the destination view controller only when the save button is pressed.
        if saveButtonPressed == true{
            let title = titleTextField.text ?? ""
            let photo = photoImageView.image
            let location = locationTextField.text
            let description = descriptionTextField.text

            // Set the meal to be passed to MealTableViewController after the unwind segue.
            event = Event(title: title, photo: photo, location: location ?? "", description: description ?? "")
        }
        else {
            os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
            return
        }

    }

//MARK: Actions

    @IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
        print("touch")
        // Hide the keyboard.
        titleTextField.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)
    }
}

实际结果:

2019-01-07 13:16:25.547164-0500 Univent2 [24331:877799]未按下保存按钮,取消了

预期的结果是保存按钮保存标题,图像,描述并将其另存为对象以通过segue返回并显示在表视图控制器上的

0 个答案:

没有答案