【问题标题】:Swift Navigate from third ViewController to the first ViewControllerSwift 从第三个 ViewController 导航到第一个 ViewController
【发布时间】: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


【解决方案1】:

你可以使用类似的东西:

func popTo(_ type: AnyClass) {
    guard let controllers = navigationController?.viewControllers else {return}
    
    for controller in controllers {
        if controller.classForCoder == type {
            navigationController?.popToViewController(controller, animated: true)
            break
        }
    }
}

并根据条件调用函数为:

popTo(A.classForCoder())

编辑:仅当 ViewControllers B 和 C 通过导航控制器呈现并且位于同一导航堆栈中时,上述解决方案才有效。

由于您以模态方式呈现 ViewController C,因此以下答案应该有效:

对呈现 C 的 ViewController B 进行这些更改:

class B: UIViewController, PresenterDelegate {
// some functions

    func popToPrevious() {
        navigationController.popViewController(animated: false)
    }

    @objc func editCell(){
        let vc = storyboard?.instantiateViewController(identifier: 
        "ModalPresentationViewController") as? 
         ModalPresentationViewController
        vc.presenterDelegate = self

        present(vc, animated: true, completion: nil)

    }
}

在你的 ViewController C 中:

class C {
    var presenterDelegate: PresenterDelegate? = nil

    //

    @objc func savePerson(){
        //
        //
        dismiss(animated: true, completion: {
            self.presenterDelegate?.popToPrevious()
        })
    }
}

还将 PresenterDelegate 协议添加到新文件或 ViewControllers B 或 C 之下

protocol PresenterDelegate {
    func popToPrevious()
}

【讨论】:

  • 感谢您的回答,这是个好主意,但仍然无法访问 A ViewController。我已经尝试了很多这样的解决方案。问题是我忘记提到的是我在关闭完成中调用的这个函数,因为 C-ViewController 是一个当前模式。所以每个解决方案都让我回到了 B - View Controller。我也尝试过 popToRootViewController 但仍然没有
  • 这正是我对stackoverflow.com/questions/44669698/… 的问题,但对我仍然不起作用
  • 嘿,Donald,由于您在图表中在 B 和 C 之间声明了“PushVC”,因此该解决方案基于 A、B、C 位于同一导航堆栈中的假设。我将编辑我的答案。
  • 是的,我的错误 B 到 C -> 呈现不推送
  • 做了一些改变,让我知道 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
相关资源
最近更新 更多