【发布时间】:2020-11-08 13:46:00
【问题描述】:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(identifier:
"PersonViewController") as? PersonViewController
vc?.names = persons[indexPath.row].emer!
vc?.lastnames = persons[indexPath.row].mbiemer!
vc?.delegate = self
PersonViewController.indexes = indexPath.row
self.navigationController?.pushViewController(vc!, animated: true)
}`
我有这样的情况:
第一个 ViewController 是一个 collectionView,第二个是一个 viewcontroller,当我点击一个按钮时它可以添加新的 Person 并且工作正常。我已将委托和核心数据用于本地内存。
第二个 ViewController 也有另一个按钮来编辑人员。当我按下按钮时,会出现一个带有扩展名UIAdaptivePresentationControllerDelegate 的新视图控制器。此视图控制器由 2 个按钮和 2 个文本字段组成。因此,当我想按保存按钮时,我想转到第一个视图控制器(collectionview 列表),何时按取消返回到第二个视图控制器。
视图控制器是使用 pushViewController 方法创建的。
请任何人帮助我应该使用什么?
然后在 PersonViewController 中,我将其称为内部按钮编辑。
@objc func editCell(){
let vc = storyboard?.instantiateViewController(identifier:
"ModalPresentationViewController") as?
ModalPresentationViewController
navigationController?.pushViewController(vc!, animated: true)
}
现在 las ViewController 中的代码是 ModalViewController
@objc func savePerson(){
if editNameTextfield.text == "" || editlastNameTextfield.text == ""{
self.errorLbl.alpha = 1
}
else{
let vc = ViewController()
guard let textName = editNameTextfield.text else{
return
}
guard let textLastName = editlastNameTextfield.text else{
return
}
let index = PersonViewController.indexes
DispatchQueue.main.async {[self] in
if editDelegate != nil{
self.editDelegate!.editPerson(editedName: textName, editedLastname: textLastName, index: index)
}
}
// What should I call here??
}
}
【问题讨论】:
-
现在已编辑。我只是想要一个指南来解决这个问题
-
您是通过代码处理导航还是在 Storyboard 中使用 Segue?
-
那么问题出在哪里?
popToViewController让你回到你想要的任何视图控制器。 developer.apple.com/documentation/uikit/uinavigationcontroller/… -
不,我没有使用 segues。在 ViewController 中只需嵌入 Navigation Controller 并使用故事板标识符推送到另一个 ViewController。此外,3 个 ViewController 中的所有视图都是由不在 Main.storyboard 中的代码创建的
-
无法使用 popToViewController 保存数据。我仅使用 popViewController 成功保存数据,但此方法使我回到前一个屏幕而不是第一个屏幕
标签: ios swift navigationcontroller