gpt4 book ai didi

ios - 如何通过 popViewController Animated for Swift 发回数据?

转载 作者:IT王子 更新时间:2023-10-29 05:05:31 27 4
gpt4 key购买 nike

我需要通过 popView 将一些数据从 secondView 发送回 First View。我如何通过 popViewControllerAnimated 发回数据?

谢谢!

最佳答案

您可以使用 delegate 传回数据

  1. ChildViewController中创建protocol
  2. ChildViewController中创建delegate变量
  3. MainViewController中扩展ChildViewController协议(protocol)
  4. navigate时引用MainViewControllerChildViewController
  5. MainViewController中定义delegate方法
  6. 然后你可以从ChildViewController调用delegate方法

示例

在 ChildViewController 中:在下面编写代码...

protocol ChildViewControllerDelegate
{
func childViewControllerResponse(parameter)
}

class ChildViewController:UIViewController
{
var delegate: ChildViewControllerDelegate?
....
}

在主视图 Controller 中

// extend `delegate`
class MainViewController:UIViewController,ChildViewControllerDelegate
{
// Define Delegate Method
func childViewControllerResponse(parameter)
{
.... // self.parameter = parameter
}
}

有两种选择:

A) 与 Segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let goNext = segue.destinationViewController as ChildViewController
goNext.delegate = self
}

B) 没有 Segue

let goNext = storyboard?.instantiateViewControllerWithIdentifier("childView") as ChildViewController
goNext.delegate = self
self.navigationController?.pushViewController(goNext, animated: true)

方法调用

self.delegate?.childViewControllerResponse(parameter)

关于ios - 如何通过 popViewController Animated for Swift 发回数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30618172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com