0

I've been trying to connect the IBOutlet's to the releasedatepicker, artisttextfield, albumtextfield, saveTapped and cancelTapped in my EditFreshReleaseViewController for a few days now. But whenever I run the app and tap the Edit button, it crashes the app. When I disconnect the IBOutlet's on that ViewController it opens the edit button fine except I'm stuck there because nothing works in that ViewController now.

Any Ideas?

IBOutlets on EditFreshReleaseViewController

Below is the code for the EditFreshReleaseViewController:

import UIKit
import CoreData
import UserNotifications

class EditFreshReleaseViewController: UIViewController {


@IBOutlet var artisttextfield: UITextField!
@IBOutlet var albumtextfield: UITextField!
@IBOutlet var releasedatePicker: UIDatePicker!

override func viewDidLoad() {
    super.viewDidLoad()

    releasedatePicker.minimumDate = Date()


    // Do any additional setup after loading the view, typically from a nib.
}
@IBAction func saveTapped( _ sender: UIBarButtonItem) {

    let artist = artisttextfield.text ?? ""
    let album = albumtextfield.text ?? ""
    let releasedate = releasedatePicker.date

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    let newRelease = Release_Date(context: context)
    newRelease.artist = artist
    newRelease.album = album
    newRelease.release_date = releasedate as NSDate?
    newRelease.release_dateId = UUID().uuidString

    if let uniqueId = newRelease.release_dateId {
        print("The freshreleaseId is \(uniqueId)")
    }

    do {
        try context.save()
        let message = "\(artist)'s new album \(album) releases Today!"
        let content = UNMutableNotificationContent()
        content.body = message
        content.sound = UNNotificationSound.default()
        var dateComponents = Calendar.current.dateComponents([.month, .day],
                                                             from: releasedate)
        dateComponents.hour = 09
        dateComponents.minute = 00
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,
                                                    repeats: true)
        if let identifier = newRelease.release_dateId {

            let request = UNNotificationRequest(identifier: identifier,
                                                content: content, trigger: trigger)
            let center = UNUserNotificationCenter.current()
            center.add(request, withCompletionHandler: nil)
        }
    } catch let error {
        print("Could not save because of \(error).")
    }

    dismiss(animated: true, completion: nil)

    print("Added a Release Date!")
    print("Artist: \(newRelease.artist)")
    print("Album: \(newRelease.album)")
    print("Release Date: \(newRelease.release_date)")
}

@IBAction func cancelTapped(_ sender: UIBarButtonItem) {
    dismiss(animated: true, completion: nil)
}



}
sckring
  • 31
  • 5
  • Show the error message, not only in a screenshot. The issue is that you didn't set the class of the ViewController in InterfaceBuilder to `EditFreshReleaseViewController`. It's still a `UIViewController` (default one). So it doesn't know the cancelTapped method. – Larme Oct 30 '18 at 17:28
  • Error comes from AppDelegate.Swift file as Thread 1: signal SIGABRT attached to the line class AppDelegate: UIResponder, UIApplicationDelegate { – sckring Oct 30 '18 at 18:10
  • But there is a message in the console. That’s the one you should read at least. And I said the reason, you didn’t set the class of the viewcontroller to yourd – Larme Oct 30 '18 at 18:11

1 Answers1

0

Without the error it is hard to figure out the solution, but you can start by making the @IBOutlet weak

@IBOutlet weak var artisttextfield: UITextField!
@IBOutlet weak var albumtextfield: UITextField!
@IBOutlet weak var releasedatePicker: UIDatePicker!

Also sometimes when you copy and paste a UIViewcontroller elements from a different UIViewcontroller the @IBOutlet's lost the references, try cleaning you project, close Xcode, restart everything.

Raul Mantilla
  • 334
  • 1
  • 5
  • 2018-10-30 15:53:27.213772-0400 fresh release[7843:2107116] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key albumtextfield.' – sckring Oct 30 '18 at 20:15
  • I already said it `[ `, it's written `UIViewController`, not `EditFreshReleaseViewController`. You didn't set the class of the ViewController in the Storyboard to `EditFreshReleaseViewController`. Last image: https://stackoverflow.com/a/28589496/1801544 – Larme Oct 30 '18 at 21:28