0

ситуация такая сделано все согласно видео, на почему то не работает, а конкретнее в конце видео создается новый объект по нажатию на + и заполнению всех полей, после нажимается save и окно закрывается а в консоле появляется " Сохранение удалось ". У меняя ситуация такая проекте билдится без ошибок, но после заполнения всей формы не работает ни cancel ни save, если формы не заполнять то выкидывает алерт и кнопка cancel по прежнему не работает. Все Unwind Segues прикручены, почему не работает не пойму. введите сюда описание изображения

import UIKit

class NewEateryTableViewController: UITableViewController,
                                    UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var adresTextField: UITextField!
    @IBOutlet weak var typeTextField: UITextField!
    @IBOutlet weak var yesButton: UIButton!
    @IBOutlet weak var noButton: UIButton!
    var isVisited  = false

    @IBAction func toggleIsVisitedPressed(_ sender: UIButton) {
        if sender == yesButton {
            sender.backgroundColor = #colorLiteral(red: 0.2745098174,
                                                   green: 0.4862745106,
                                                   blue: 0.1411764771,
                                                   alpha: 1)
            noButton.backgroundColor = #colorLiteral(red: 0.501960814,
                                                     green: 0.501960814,
                                                     blue: 0.501960814,
                                                     alpha: 1)
            isVisited = true
        } else {
            sender.backgroundColor = #colorLiteral(red: 0.7450980544,
                                                   green: 0.1568627506,
                                                   blue: 0.07450980693,
                                                   alpha: 1)
            yesButton.backgroundColor = #colorLiteral(red: 0.501960814,
                                                      green: 0.501960814,
                                                      blue: 0.501960814,
                                                      alpha: 1)
            isVisited = false
        }
    }

    @IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
        if nameTextField.text == "" || adresTextField.text == "" ||
            typeTextField.text == "" {
            let alert = UIAlertController(title: "Ошибка",
                                          message: "Ну все поля заплнены",
                                          preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Ok",
                                          style: .default,
                                          handler: nil))
            self.present(alert, animated: true, completion: nil)
        } else {
            if let context =  (UIApplication.shared.delegate as? AppDelegate)?.coreDataStack.persistentContainer.viewContext{
                let reustarant = Restaurant(context: context)
                reustarant.name = nameTextField.text
                reustarant.location = adresTextField.text
                reustarant.type = typeTextField.text
                reustarant.isVisited = isVisited
                if let image = imageView.image {
                    reustarant.image = image.pngData()
                }
                do {
                    try context.save()
                    print(" Сохранение удалось ")
                } catch let error as NSError {
                    print(" Не удалось сохранить данные\(error), \(error.userInfo)")
                }
            }
            performSegue(withIdentifier: "unwindSegueFromNewEatery",
            sender: self)
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        yesButton.backgroundColor = #colorLiteral(red: 0.2745098174,
                                                  green: 0.4862745106,
                                                  blue: 0.1411764771,
                                                  alpha: 1)
        noButton.backgroundColor = #colorLiteral(red: 0.7450980544,
                                                 green: 0.1568627506,
                                                 blue: 0.07450980693,
                                                 alpha: 1)
    }

    private func imagePickerController(_picker: UIImagePickerController,
                                       didFinishPickingMediaWithInfo info: [String : Any]) {
        imageView.image =
            info[UIImagePickerController.InfoKey.editedImage.rawValue] as?
            UIImage
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        dismiss(animated: true, completion: nil)
    }

    override func tableView(_ tableView: UITableView,
                            didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            let alertController = UIAlertController(
                title: "Источник фотографии",
                message: nil,
                preferredStyle: .actionSheet)
            let cameraAction = UIAlertAction(title: "Камера", style: .default,
                                             handler: { (action) in
                                                self.chooseImagePickerAction(source: .camera)
                                             })
            let photoLibAction = UIAlertAction(title: "Фото", style: .default,
                                               handler: { (action) in
                                                self.chooseImagePickerAction(source: .photoLibrary)
                                               })
            let cancelAction = UIAlertAction(title: "Отмена", style: .cancel,
                                             handler: nil)
            alertController.addAction(cameraAction)
            alertController.addAction(photoLibAction)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }

        tableView.deselectRow(at: indexPath, animated: true)
    }

    func chooseImagePickerAction(source: UIImagePickerController.SourceType) {
        if UIImagePickerController.isSourceTypeAvailable(source) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.allowsEditing = true
            imagePicker.sourceType = source
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
}
4
  • по картинке не гадаем) приложите код
    – Andrew
    7 ноя 2020 в 10:18
  • да я понимаю что не гадаете. Добавил
    – Роман
    7 ноя 2020 в 10:32
  • что-то не видно пока-что
    – Andrew
    7 ноя 2020 в 10:37
  • теперь все вроде ок
    – Роман
    7 ноя 2020 в 10:47

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.