将代码连接到情节提要时,应用程序崩溃

时间:2018-10-30 17:25:27

标签: swift viewcontroller iboutlet

几天来,我一直在尝试将IBOutlet的连接到EditFreshReleaseViewController中的releaseatepicker,artisttextfield,albumtextfield,saveTapped和cancelTapped。但是,每当我运行该应用程序并点击“编辑”按钮时,它就会崩溃。当我断开该ViewController上的IBOutlet的连接时,它会很好地打开“编辑”按钮,除非我被卡在那里,因为现在该ViewController中没有任何功能。

有什么想法吗?

IBOutlets on EditFreshReleaseViewController

下面是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)
}



}

1 个答案:

答案 0 :(得分:0)

没有错误,很难找出解决方案,但是您可以先使@IBOutlet变弱

{{1}}

有时,当您从另一个UIViewcontroller复制并粘贴UIViewcontroller元素时,@ IBOutlet会丢失引用,请尝试清理您的项目,关闭Xcode,重新启动所有内容。