【发布时间】:2020-12-27 16:06:17
【问题描述】:
我正在尝试创建一个应用程序,它采用初始条件并将其保存在 Realm 数据库中。 这是我的 Realm 类的代码:
class FunctionData: Object {
@objc dynamic var Name = ""
@objc dynamic var status = false
@objc dynamic var statusComment = ""
@objc dynamic var tester = ""
override static func primaryKey() -> String? {
return "functionNames"
}
}
我创建了一个 TableViewController,并将“名称”作为每个单元格的名称。我想单击单元格并转到“SecondViewController”并在那里填写其他信息,例如测试人员和评论。 TableViewController 运行良好。 这里的代码,使 segue 到 SecondViewController:
var functions: Results<FunctionData>?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let function = functions![indexPath.row]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let functionsVC = storyboard.instantiateViewController(identifier: "functionsVC") as! FunctionsRatingViewController
functionsVC.name = function.functionNames
functionsVC.functionID = function.functionNames
if let indexPath = tableView.indexPathForSelectedRow {
functionsVC.selectedFunction = functions![indexPath.row]
}
self.present(functionsVC, animated: true, completion: nil)
}
但在我的 SecondVC 中,我无法将其他信息保存在选定的“项目”中。我正在尝试使用 AlertController 来做到这一点
var selectedFunction: FunctionData? {
didSet {
}
}
func showAlert() {
var textField = UITextField()
let alert = UIAlertController(title: "Status not OK", message: nil, preferredStyle: .alert)
alert.addTextField {
$0.placeholder = "Comment"
$0.addTarget(alert, action: #selector(alert.textDidChangeInLoginAlert), for: .editingChanged)
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
let loginAction = UIAlertAction(title: "Save", style: .default) { [unowned self] _ in
if let currentFct = self.selectedFunction {
do {
try self.realm.write {
currentFct.realm?.create(FunctionData.self, value: ["statusComment": textField.text], update: .modified)
}
} catch {
print("Error trying to append a Comment, \(error)")
}
}
print("Comment saved")
}
loginAction.isEnabled = false
alert.addAction(loginAction)
present(alert, animated: true)
}
非常感谢您的帮助!
【问题讨论】:
标签: ios swift uiviewcontroller realm