我开始学习编程,因此决定尝试我的第一个笔记记录应用程序。
我的目标是创建类似于iPhone的NoteApp的应用程序。因此,我希望在用户将TextView作为第一行写入时设置注释的标题。因此,我创建了一个NoteViewController
,其中包含一个TextView和一个NoteIndexViewController
,这是一个TableViewController
,两者都嵌入在NavigationController
中。
我还使用Core Data来存储数据。
问题是我不知道如何在不使用按钮的情况下将这些更改提交到数据库。我知道如何创建NSManagedObject
的实例-在NoteIndexViewController
中使用[Button]在TableView
中创建新的音符:
@IBAction func addNotePressed(_ sender: UIBarButtonItem) {
let newNoteIndex = NoteIndex(context: self.context)
newNoteIndex.name = "Temporal Name"
notesArray.append(newNoteIndex)
saveNoteIndex()
performSegue(withIdentifier: K.segueToNote, sender: self)
}
但是如果我要提交更改而没有“保存按钮”来创建实例并提交更改,我将完全迷失。这是我到目前为止得到的代码。 通知我没有设置任何Note()
对象。
class NoteViewController: UIViewController {
var noteArray = [Note]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var selectedNote: NoteIndex? {
didSet {
loadData()
}
}
var firstLine: String?
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
if !textView.text.isEmpty {
if let newLine = textView.text.firstIndex(of: "\n") {
let firstLetter = textView.text.startIndex
let lineBrake = textView.text.index(before: newLine)
let lettersTillPosition = textView.text.distance(from: firstLetter, to: lineBrake)
firstLine = (textView.text as NSString).substring(to: lettersTillPosition)
} else {
if textView.text.count >= 30{
firstLine = (textView.text as NSString).substring(to: 30)
} else {
firstLine = (textView.text as NSString).substring(to: textView.text.count)
}
}
selectedNote!.name = firstLine
saveCurrentNote()
}
}
//MARK: - Data Manipulation Methods
func saveCurrentNote() {
do {
try context.save()
} catch {
print("Error saving cateogry \(error)")
}
}
func loadData(with request: NSFetchRequest<Note> = Note.fetchRequest()) {
// goToIndex is the relationship between the IndexNote entity and Note. And when Back button is pressed the code tend also to break in this part.
request.predicate = NSPredicate(format: "goToIndex.name MATCHES %@", selectedNote!.name!)
do {
noteArray = try context.fetch(request)
} catch {
print("This is a load error: \(error)")
}
}
}
extension NoteViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
saveCurrentNote()
}
}
这里是您问题的可能解决方案。您可以使用通知中心来监视用户是否被打扰,如果是这样,则可以进行快速保存。
将它们放置在场景委托中
func sceneWillResignActive(_ scene: UIScene) {
let notificationName = NSNotification.Name(ReuseIdentifier.pause)
NotificationCenter.default.post(name: notificationName , object: nil)
}
func sceneDidDisconnect(_ scene: UIScene) {
let notificationName = NSNotification.Name(ReuseIdentifier.quit)
NotificationCenter.default.post(name: notificationName, object: nil)
}
将类似的内容放置在保存用户数据的位置。
/// Monitors application state for major changes.
/// - Pause Observer: Adds observer that notifies application if application is no longer active (enters foreground).
/// - Quit Observer: Adds observer that notifies application if terminated.
private func checkForPauseOrQuit(){
NotificationCenter.default.addObserver(self,
selector: #selector(autoSave),
name: NSNotification.Name(ReuseIdentifier.pause),
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(autoSave),
name: NSNotification.Name(ReuseIdentifier.quit),
object: nil)
}
然后为您的选择器方法创建NSManagedObject并捕获用户可能已开始键入的任何值。
在启动时,您要进行相反的操作,并确保擦除值。这仅应作为临时存放容器,而不能作为您的主要实体。查看我的笔记应用程序以供参考:
https://github.com/victis23/AwesomeNote/tree/WorkingBranch/AwesomeNote