我知道周围有很多类似的问题--我担心我的问题是在如此基本的层面上,我能找到的所有其他问题都要复杂得多,然而,似乎不能完全回答这个问题。
我正在我的日记应用程序中实现“设置”-我需要让用户能够编辑(在其他事情中)日记的名称。
为了理解我的问题:对于应用程序的初学者,你有一个包含所有当前日记的tableView -然后当你输入一个特定的日记时,你就可以在这里找到“设置”来编辑这个特定的日记。
在这里,当单击名为“日记名称”的tableViewCell时,输入一个简单的viewController,其中包含一个textField (保存您当前的日记名称)和一个按钮,该按钮将弹出您并将日记名称更改为此textField中的新输入。
下面是我在这个“保存按钮”上的当前代码:
@IBAction func saveChanges(_ sender: Any) {
let newDiaryName = Diaries() // Here, I need to reach the specific diary that we are currently editing!
diary?.diaryName = changeDiaryNameTextField.text!
let realm = try! Realm()
try! realm.write {
realm.add(newDiaryName)
}
self.navigationController?.popViewController(animated: true)
}
}所以我很抱歉,但我是个新手...
我的问题是:在地狱里,我怎么才能到达我现在所在的特定日记?-当我想要更新一个对象时,这个函数甚至是正确的吗?
回到包含所有日记列表的第一个tableView中,我将日记保存如下:
(所以我猜一本日记有点像allDiariesindexPath..但我如何从整个其他viewController中实现这一点呢?)
let allDiaries: Results<Diaries>
required init?(coder aDecoder: NSCoder) {
let realm = try! Realm()
allDiaries = realm.objects(Diaries.self).sorted(byKeyPath: String("dateCreated"), ascending: false)
super.init(coder: aDecoder)
}很抱歉这么长的解释,我不知道该怎么问!
谢谢!
发布于 2018-11-09 18:15:32
假设保存名称为DairyViewControll的Dairy视图控制器,并从DairiesViewController打开它。因为我不知道你是展示它还是推(Storyboard segue),我将展示这两种情况。
主要的想法是在呈现之前分配你的乳制品对象。
class DairiesViewController: UIViewController {
//open selected dairy, the first option if you present it, second via segue
1. present
func openDairy(diary: Dairy) {
let storyboard = UIStoryboard(name: "main", bundle: nil) //please make sure your storyboard is "main" otherwise change name
let dairyVC = storyBoard.instantiateViewController(withIdentifier: "dairyIdentifier") as! DairyViewControll
dairyVC.dairy = dairy
self.present(dairyVC, animated: true, completion: nil)
}
}
2. via segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let dairyVC = segue.destination as! DairyViewControll
dairyVC.dairy = self.dairy // holds selected dairy
}
class DairyViewControll: UIViewController {
let dairy: Dairy! // the dairy user selects to edit
@IBAction func saveChanges(_ sender: Any) {
// you save method is here
}
}https://stackoverflow.com/questions/53222815
复制相似问题