iOS Xcode (swift) - unwind segue 后如何执行代码
iOS Xcode (swift) - how to execute code after unwind segue
我执行从场景 1 到场景 2 的转场。然后我 return 从场景 2 到场景 1。我如何不仅将数据从场景 2 传递到场景 1,而且在场景 1 中检测到我return从场景 2 开始执行场景 1 中的代码了吗?
在 Android 中,我使用 startActivity 和 onActivityResult 执行此操作。
你可以这样做:
class SourceViewController: UIViewController {
var didReturnFromDestinationViewController = false
@IBAction func returnToSourceViewController(segue: UIStoryboardSegue) {
didReturnFromDestinationViewController = true
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if didReturnFromDestinationViewController == true {
// reset the value
didReturnFromDestinationViewController = false
// do whatever you want
}
}
}
像其他答案建议的那样引入 Bool
状态 非常糟糕 并且必须尽可能避免,因为它会大大增加应用的复杂性。
在许多其他模式中,解决此类问题最简单的方法是将 delegate
对象传递给 Controller2
。
protocol Controller2Delegate {
func controller2DidReturn()
}
class Controller1: Controller2Delegate {
func controller2DidReturn() {
// your code.
}
func prepareForSegue(...) {
// get controller2 instance
controller2.delegate = self
}
}
class Controller2 {
var delegate: Controller2Delegate!
func done() {
// dismiss viewcontroller
delegate.controller2DidReturn()
}
}
States are evil 并且是软件错误的最大来源。
我遇到的问题是我试图在 unwind segue 完成后显示一个警告对话框。所以我的 View Controller 2 对 View Controller 1 执行了 unwind segue。我发现在 unwind segue 方法之后 运行s 的代码在 View Controller 2 被关闭之前被调用 运行s,所以当我试图显示一个警告对话框,它会在 View Controller 2 被关闭后立即消失。
如果其他解决方案不适合您,请执行我所做的。我向我的 class 添加了一个 viewWillAppear 覆盖并关闭了那里的父控制器,然后添加了我的警报对话框的代码。为了确保 viewWillAppear 没有在第一次显示 View Controller 1 时显示警报对话框,我设置了一个 if 语句来检查我在 class 中声明并设置为等于“”的变量的名称.然后在 View Controller 2 中,我将变量中的一些文本传递回 View Controller 1,所以当 if 语句 运行s 它测试变量不等于“”,当它发现它不等于时,代码是 运行。在我的例子中,变量被命名为 "firstname".
override func viewWillAppear(_ animated: Bool) {
if firstname != "" {
self.parent?.dismiss(animated: true, completion: nil)
//CustomViewController?.dismiss(animated: true, completion: nil)
let alertController = UIAlertController(title: "Hello", message: "This is a test", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Close Alert", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}
我执行从场景 1 到场景 2 的转场。然后我 return 从场景 2 到场景 1。我如何不仅将数据从场景 2 传递到场景 1,而且在场景 1 中检测到我return从场景 2 开始执行场景 1 中的代码了吗?
在 Android 中,我使用 startActivity 和 onActivityResult 执行此操作。
你可以这样做:
class SourceViewController: UIViewController {
var didReturnFromDestinationViewController = false
@IBAction func returnToSourceViewController(segue: UIStoryboardSegue) {
didReturnFromDestinationViewController = true
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if didReturnFromDestinationViewController == true {
// reset the value
didReturnFromDestinationViewController = false
// do whatever you want
}
}
}
像其他答案建议的那样引入 Bool
状态 非常糟糕 并且必须尽可能避免,因为它会大大增加应用的复杂性。
在许多其他模式中,解决此类问题最简单的方法是将 delegate
对象传递给 Controller2
。
protocol Controller2Delegate {
func controller2DidReturn()
}
class Controller1: Controller2Delegate {
func controller2DidReturn() {
// your code.
}
func prepareForSegue(...) {
// get controller2 instance
controller2.delegate = self
}
}
class Controller2 {
var delegate: Controller2Delegate!
func done() {
// dismiss viewcontroller
delegate.controller2DidReturn()
}
}
States are evil 并且是软件错误的最大来源。
我遇到的问题是我试图在 unwind segue 完成后显示一个警告对话框。所以我的 View Controller 2 对 View Controller 1 执行了 unwind segue。我发现在 unwind segue 方法之后 运行s 的代码在 View Controller 2 被关闭之前被调用 运行s,所以当我试图显示一个警告对话框,它会在 View Controller 2 被关闭后立即消失。
如果其他解决方案不适合您,请执行我所做的。我向我的 class 添加了一个 viewWillAppear 覆盖并关闭了那里的父控制器,然后添加了我的警报对话框的代码。为了确保 viewWillAppear 没有在第一次显示 View Controller 1 时显示警报对话框,我设置了一个 if 语句来检查我在 class 中声明并设置为等于“”的变量的名称.然后在 View Controller 2 中,我将变量中的一些文本传递回 View Controller 1,所以当 if 语句 运行s 它测试变量不等于“”,当它发现它不等于时,代码是 运行。在我的例子中,变量被命名为 "firstname".
override func viewWillAppear(_ animated: Bool) {
if firstname != "" {
self.parent?.dismiss(animated: true, completion: nil)
//CustomViewController?.dismiss(animated: true, completion: nil)
let alertController = UIAlertController(title: "Hello", message: "This is a test", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Close Alert", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}