【问题标题】:How can I Segue back, to a different view controller, and then forward, to another view controller我怎样才能转回,到另一个视图控制器,然后转发到另一个视图控制器
【发布时间】:2018-04-13 16:27:25
【问题描述】:

我是第一次涉足 iOS 应用程序开发,我正在考虑一个项目,目前正在处理布局。

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

应用程序启动到 VC1,单击搜索按钮,会弹出一个模式 VC2,其中包含最近的和搜索栏。你输入一个名字并点击搜索,这基本上是我希望它继续回到 VC1,然后转发到 VC3(应用程序的播放器屏幕)

现在它变成了VC1(SearchButtonAction) -> VC2(SearchPlayerAction) -> VC3(但是从模态到视图控制器的转换看起来很奇怪,如果我回击它看起来更奇怪。

我想要它去

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

我真的不知道如何管理它,否则我会附上一些代码。相反,我附上了我目前所做工作的屏幕截图。

我不确定我是否应该做类似prepareForSegue 的事情并制作一个布尔值来标记它是否应该在加载时自动继续发送到 VC3,但是我需要将数据传递给 VC1,只是为了将它传回 VC3,它看起来很乱,我只想将相同的数据从 VC2 传递回 VC3,尽管通过 VC1。我希望这很清楚 x.x

【问题讨论】:

  • 点击搜索后,我猜你关闭了 VC2 并且 VC1 仍然存在。您可以在 VC1 中添加具有时间延迟的函数以实现平滑过渡 func xxxx() { DispatchQueue.main.asyncAfter(deadline: seconds), performSegue for VC3 }。有几种方法可以调用这个函数。

标签: ios swift xcode segue


【解决方案1】:

您可以使用一些选项。

1.放松转场

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

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

示例

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。委托模式

VC2 可以创建一个 VC1 可以遵循的协议。在关闭 VC2 时调用此协议函数。

示例

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)
  }
}

【讨论】:

    猜你喜欢
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 2016-09-06
    相关资源
    最近更新 更多