在我弹出之前,我想将bool设置为true.由于ViewController在内存中,因此实例化另一个视图控制器似乎是错误的.
最好的方法是什么?我不使用故事板,如果这对你的答案很重要.
谢谢你的帮助
在SecondViewController中,在类声明之上,添加以下代码:
protocol SecondVCDelegate { func didFinishSecondVC(controller: SecondViewController) }
然后在SecondViewContoller里面添加一个类变量:
var delegate:MeditationVCDelegate! =没有
然后在你的按钮所针对的函数内部添加:
self.navigationController?.popViewControllerAnimated(true) delegate.didFinishSecondVC(self)
我们在这里做的是在SecondViewController中执行pop,而不传递任何数据,但由于我们已经定义了一个协议,我们将在ViewController中使用它来处理数据.
接下来,在ViewController中,将您在SecondViewController中定义的协议添加到ViewController继承的类列表中:
class ViewController: UIViewController,SecondVCDelegate { ... your code... }
您需要添加我们在新协议中定义的函数,以使编译器满意.在ViewController的类中,添加以下内容:
func didFinishSecondVC(controller: SecondViewController) { self.myBoolVar = true controller.navigationController?.popViewControllerAnimated(true) }
在我们调用didFinishSecondVC的SecondViewController中,我们在ViewController类中调用此方法,我们正在弹出的控制器.它类似于我们在SecondViewController中编写这个代码,但是我们已经在ViewController中编写了它,我们正在使用委托来管理两者之间的消息传递.
最后,在我们的目标是推送到SecondViewController的函数中,添加以下代码:
let secondVC = secondViewController() secondVC.delegate = self self.navigationController?.pushViewController(secondVC,animated: true)
而已!您应该设置为在两个视图控制器之间传递代码而不使用故事板!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。