Mac OSX应用程序:与NSArrayController相关的问题NSTableView核心数据添加记录

时间:2018-02-28 11:15:56

标签: objective-c cocoa core-data nstableview nsarraycontroller

我使用NSArrayController NSTableViewCore data 绑定

我已将button与[{1}}方法联系起来add:

关于添加新记录

NSArrayController添加并显示新记录 TableView NSArrayController's:名为

的方法

问题: 值不会添加到核心数据中(Sqlite类型)。

应用程序重新启动显示旧数据。

2 个答案:

答案 0 :(得分:0)

这是一个苹果示例代码。基本上它会尝试在应用程序终止之前保存上下文。根据您的具体情况,您可以将功能移至其他位置。

func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
    // Save changes in the application's managed object context before the application terminates.
    let context = persistentContainer.viewContext

    if !context.commitEditing() {
        NSLog("\(NSStringFromClass(type(of: self))) unable to commit editing to terminate")
        return .terminateCancel
    }

    if !context.hasChanges {
        return .terminateNow
    }

    do {
        try context.save()
    } catch {
        let nserror = error as NSError

        // Customize this code block to include application-specific recovery steps.
        let result = sender.presentError(nserror)
        if (result) {
            return .terminateCancel
        }

        let question = NSLocalizedString("Could not save changes while quitting. Quit anyway?", comment: "Quit without saves error question message")
        let info = NSLocalizedString("Quitting now will lose any changes you have made since the last successful save", comment: "Quit without saves error question info");
        let quitButton = NSLocalizedString("Quit anyway", comment: "Quit anyway button title")
        let cancelButton = NSLocalizedString("Cancel", comment: "Cancel button title")
        let alert = NSAlert()
        alert.messageText = question
        alert.informativeText = info
        alert.addButton(withTitle: quitButton)
        alert.addButton(withTitle: cancelButton)

        let answer = alert.runModal()
        if answer == .alertSecondButtonReturn {
            return .terminateCancel
        }
    }
    // If we got here, it is time to quit.
    return .terminateNow
}

答案 1 :(得分:0)

你可能不想在NSArrayController上设置managedObjectContextentityName