我正在尝试在 viewWillDisappear 函数中使用 unwind segue 更改标签文本。每次调用 viewWillDisappear 函数时,它都会自动跳过我的 unwind segue 行而不是执行它。
我正在使用 Xcode 9 和 Swift 4.2
这些是我的代码:
主页面ViewController.swift
import UIKit
import Foundation
struct globalConstant {
let lostMessage : String = "You Lost!"
let welcomeMessage : String = "WELCOME"
let playAgainMessage : String = "Play again"
let resumeMessage : String = "Resume"
let pausedMessage : String = "Game is paused"
let playMessage : String = "Play Game"
}
class ViewController: UIViewController {
var globalConst = globalConstant()
var labelValue : String!
//start Outlet
@IBOutlet weak var mainLabel: UILabel!
@IBOutlet weak var startButton: UIButton!
//end Outlet
@IBAction func unwindFromGame(_ sender: UIStoryboardSegue){
let gameController = sender.source as? gamePageViewController
if (gameController?.balVariables.balanceValue)! <= 0{
mainLabel.text = gameController?.balVariables.abc
}
else {
mainLabel.text = self.globalConst.pausedMessage
}
}
}
我的第二页gamePageViewController.swift
import UIKit
import Foundation
struct balanceVariables {
//declare var start
var initBalance : Int = 500
var balanceValue : Int = 0
let abc = "abc"
//declare var end
}
class gamePageViewController: UIViewController {
// override func addChild(_ gamePageViewController: UIViewController) {
// }
@IBOutlet weak var valueHolder: UILabel!
var balVariables = balanceVariables()
override func viewDidLoad() {
super.viewDidLoad()
//run on page load
if balVariables.balanceValue <= 0 {
balVariables.balanceValue = balVariables.initBalance
valueHolder.text = String(balVariables.balanceValue)
}else{
balVariables.balanceValue = balVariables.balanceValue
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
performSegue(withIdentifier: "unwindFromGame", sender: self)
}
// override func didMove(toParent ViewController: UIViewController?){
// performSegue(withIdentifier: "unwindFromGame", sender: self)
// }
这是我的 Storyboard配置的图像
(对于 unwind segue 连接,我按住 Ctrl 并拖动了 View Controller 图标
在 gamePageViewController
中退出图标)
如前所述,我无法让 performSegue 在 viewWillDisappear
中运行。
我在这里想要实现的是,当我单击 NavigationBar 上的后退按钮时,viewWillDisappear
将拦截并向调用 unwindFromGame
并更改的主 ViewController 执行展开转场mainLabel
.
从 gameViewController.swift
中的代码中,您会注意到我已经注释掉了 didMove(toParent)
。使用这种方法,我可以让我的 performSegue
运行,它成功地从 gamePageViewController
中检索了 abc
变量,但问题是它加载了页面但没有等待我在后退按钮上的提示。相反,它会自动运行 didMove(toParent)
并将我发送回我的主屏幕,并编辑了 mainLabel
。我也尝试了 willMove(toParent)
并取得了相同的结果。
我的问题是:
如何让 viewWillDisappear
中的 performSegue
运行?
这是与 iPhone X/iPhone Xs Max 相关的问题吗?
使用 didMove(toParent)
或 willMove(toParent)
,如何让它等待后退按钮上的提示?
假设它无法与 NavigationBar 后退按钮一起使用。自定义 NavigationBar 后退按钮是否会达到预期效果?如果是这样,我应该怎么做?
您是否建议切换到 Swift 4 而不是 4.2?
非常感谢。
最佳答案
让我们尝试使用委托(delegate)模式而不是使用 unwind segue。
首先为您的gamePageViewController
创建委托(delegate)协议(protocol)并声明用于发送balanceVariables
的函数
protocol GamePageDelegate {
func sendBalVariable(_ variable: balanceVariables)
}
现在在您的 gamePageViewController
中创建 delegate
变量
class gamePageViewController: UIViewController {
var delegate: GamePageDelegate?
...
}
然后将此委托(delegate)实现到您的 ViewController
并声明当您在委托(delegate)上调用此方法时应该发生什么
class ViewController: UIViewController, GamePageDelegate {
...
func sendBalVariable(_ variable: balanceVariables) {
if variable.balanceValue <= 0 {
mainLabel.text = variables.abc
} else {
mainLabel.text = self.globalConst.pausedMessage
}
/* mainLabel.text = variable.balanceValue <= 0 ? variables.abc : self.globalConst.pausedMessage */
}
}
现在覆盖 ViewController
中的 prepare(for:sender:)
并将目的地的 delegate
设置为 self
(如果 segue 的标识符与 ViewController
和 gamePageViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourIdentifier" {
let destinationVC = segue.destination as! gamePageViewController
destinationVC.delegate = self
}
}
最后,在您的 gamePageViewController
委托(delegate)方法中,当 View 消失并传递 ass 参数时 balVariables
override func viewWillDisappear(_ animated: Bool) {
delegate?.sendBalVariable(balVariables)
}
关于单击 NavigationBar 后退按钮上的 viewWillDisappear 中的 Swift unwind segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53836064/