Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 快速从第三个ViewController导航到第一个ViewController_Ios_Swift_Navigationcontroller - Fatal编程技术网

Ios 快速从第三个ViewController导航到第一个ViewController

Ios 快速从第三个ViewController导航到第一个ViewController,ios,swift,navigationcontroller,Ios,Swift,Navigationcontroller,我有一个类似这样的情况: 第一个ViewController是一个collectionView,第二个是一个ViewController,当我点击一个按钮时,它可以添加新的人物,并且工作得非常好。我已经将委托和核心数据用于本地内存 另外,第二个ViewController还有另一个用于编辑人员的按钮。当我按下按钮时,会出现一个新的viewController,扩展名为UIAdaptivePresentationControllerDelegate。此viewcontroller由2个按钮和2个文

我有一个类似这样的情况:

第一个ViewController是一个collectionView,第二个是一个ViewController,当我点击一个按钮时,它可以添加新的人物,并且工作得非常好。我已经将委托和核心数据用于本地内存

另外,第二个ViewController还有另一个用于编辑人员的按钮。当我按下按钮时,会出现一个新的viewController,扩展名为UIAdaptivePresentationControllerDelegate。此viewcontroller由2个按钮和2个文本字段组成。因此,当我想按save按钮时,我想转到第一个viewcontroller(collectionview列表),当我想按cancel返回第二个viewcontroller时

视图控制器是使用pushViewController方法创建的

请任何人帮忙我该用什么

然后在PersonViewController中,我称之为内部按钮编辑

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)

}`
现在,las ViewController中的代码是ModalViewController

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

     navigationController?.pushViewController(vc!, animated: true)
    
}

您可以使用以下内容:

@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??
       
    }
}
并根据条件调用函数,如下所示:

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
        }
    }
}
编辑:只有当通过导航控制器显示的ViewController B和C位于同一导航堆栈中时,上述解决方案才有效

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

对显示C的ViewController B进行以下更改:

popTo(A.classForCoder())
在ViewController C中:

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)

    }
}
还可以将PresenterDelegate协议添加到新文件或ViewController B或C下面

class C {
    var presenterDelegate: PresenterDelegate? = nil

    //

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

现在它被编辑了。我只是想要一个解决这个问题的指南,你是通过代码来处理你的导航,还是在情节提要中使用分段?那么问题出在哪里
popToViewController
允许您返回到所需的任何视图控制器。不,我不使用segues。在ViewController中,只需嵌入导航控制器,并使用情节提要标识符推送到另一个ViewController。此外,3个ViewController中的所有视图都是由不在Main中的代码创建的。Storyboard无法使用popToViewController保存数据。我仅使用popViewController成功保存数据,但此方法将我带回上一个屏幕,而不是第一个屏幕感谢您的回答,这是一个好主意,但仍然无法到达a ViewController。我尝试过很多类似的解决方案。问题是我忘了提到这个函数,因为C-ViewController是一个present模式,所以我在discouse完成中调用它。因此,每个解决方案都将我带回B-View控制器。我也尝试过popToRootViewController,但仍然没有什么这正是我的问题所在,但对我来说仍然不起作用Hey Donald,因为您在图表中说明了B和C之间的“PushVC”,此解决方案基于A、B、C在同一导航堆栈中的假设。我将编辑我的答案。是的,将我的错误B修改为C->当前未更改,请告诉我:)
protocol PresenterDelegate {
    func popToPrevious()
}