首页 文章

我怎样才能回到另一个视图控制器,然后转发到另一个视图控制器

提问于
浏览
0

我第一次涉足iOS应用程序开发,我有一个项目,我正在进行布局 .

基本上我有一个主视图控制器和其他(我们只是称它们为VC1,VC2等...为了清晰起见)

应用程序启动到VC1,单击搜索按钮,弹出带有最近的模式VC2和搜索栏 . 你键入一个名字并点击搜索,这基本上是我想要它的位置返回VC1,然后转发到VC3(应用程序的播放器屏幕)

现在它变得 VC1(SearchButtonAction) -> VC2(SearchPlayerAction) -> VC3 (但是从模态到视图控制器的过渡看起来很奇怪,如果我回击它看起来更怪异 .

我希望它去

VC1(SearchButtonAction) -> VC2(SearchPlayerAction) -> VC1 -> VC3

我真的不知道如何管理它,或者我会附上一些代码 . 相反,我附上了截至目前为止我工作的截图 .

我应该做 prepareForSegue 之类的事情并做一个布尔值来标记它是否应该在加载时自动发送到VC3,但后来我需要将数据传递给VC1,只是将它传递回VC3,看起来很乱,我'd just like to segue / pass the same data from VC2 back to VC3, going through VC1 though. I hope that'清楚xx

enter image description here

1 回答

  • 3

    您可以使用一些选项 .

    1. Unwind Segue

    我最近开始使用它们,它们对于将数据从一个VC传递回另一个VC非常有用 . 你基本上在VC中添加了一个你要展开的函数,在你的情况下这将是VC1 . 当您关闭VC2时,将调用VC1中的此功能 . 然后你可以从这里推送到VC3 .

    我附上了一个简短的代码示例,但here is a link to a good tutorial that will describe it better.

    EXAMPLE

    VC2

    class ViewController2: UIViewController 
    {
      let dataToPassToVC3 = "Hello"
    
      @IBAction func dismissButtonTapped(_ sender: Any) 
      {
        self.dismiss(animated: true, completion: nil)
      }
    }
    

    VC1

    class ViewController1: UIViewController 
    {
      @IBAction func unwindFromVC2(segue:UIStoryboardSegue) 
      {
        //Using the segue.source will give you the instance of ViewController2 you 
        //dismissed from. You can grab any data you need to pass to VC3 from here.
    
        let VC2 = segue.source as! ViewController2 
        VC3.dataNeeded = VC2.dataToPassToVC3
    
        self.present(VC3, animated: true, completion: nil)
      }
    }
    

    2. Delegate Pattern

    VC2可以创建VC1可以遵循的协议 . 在解除VC2时调用此协议功能 .

    EXAMPLE

    VC2

    protocol ViewController2Delegate 
    {
      func viewController2WillDismiss(with dataForVC3: String)
    }
    
    class ViewController2: UIViewController 
    {
      @IBAction func dismissButtonTapped(_ sender: Any) 
      {
        delegate?.viewController2WillDismiss(with: "Hello")
        self.dismiss(animated: true, completion: nil)
      }
    }
    

    VC1

    class ViewController1: UIViewController, ViewController2Delegate 
    {
      func viewController2WillDismiss(with dataForVC3: String) 
      {
        VC3.dataNeeded = dataForVC3 
        self.present(VC3, animated: true, completion: nil)
      }
    }
    

相关问题