如何在 popViewController 之后执行第二个 ViewController 的函数?
How to execute a function from second ViewController after popViewController?
我是一名 Android 开发人员,正在尝试将我的应用程序移植到 iOS,这是我第一次使用 Swift 和 Xcode,或任何 [=20] =] 一般来说。该应用程序用于扫描二维码,如果二维码无效,那么我希望它返回到上一个屏幕并显示祝酒词。我已经做了一个功能,显示提示二维码无效。
func found(code: String) {
print(code)
// convert JSON to object
do {
let qrMessage = try JSONDecoder().decode(QrMessage.self, from: code.data(using: .utf8)!)
print("QrMessage object - device name: " + qrMessage.pcName)
}
catch let error {
print(error)
_ = navigationController?.popViewController(animated: true)
// call ShowInvalidQrToast() from the new VC that is now on top of the ViewController stack
}
编辑:
我设法从该线程的第一个答案中找到使用 NotificationCenter 的解决方案:Calling function from another ViewController in swift。我在下面的回答中有更多信息。如果您认为有更好的方法,请post您自己的答案。
我的解决方案使用NotificationCenter:
在ConnectViewController中
override func viewDidLoad() {
super.viewDidLoad()
...
NotificationCenter.default.addObserver(self, selector: #selector(showInvalidQrCodeToast(_:)), name: Notification.Name(rawValue: "showInvalidQrCodeToast"), object: nil)
}
@objc func showInvalidQrCodeToast(_ notification: Notification) {
self.view.makeToast("Invalid QR code")
}
在ScannerViewController中
func found(code: String) {
print(code)
// convert JSON to object
do {
let qrMessage = try JSONDecoder().decode(QrMessage.self, from: code.data(using: .utf8)!)
print("QrMessage object - device name: " + qrMessage.pcName)
}
catch let error {
print(error)
_ = navigationController?.popViewController(animated: true)
NotificationCenter.default.post(name: Notification.Name(rawValue: "showInvalidQrCodeToast"), object: nil)
}
我是一名 Android 开发人员,正在尝试将我的应用程序移植到 iOS,这是我第一次使用 Swift 和 Xcode,或任何 [=20] =] 一般来说。该应用程序用于扫描二维码,如果二维码无效,那么我希望它返回到上一个屏幕并显示祝酒词。我已经做了一个功能,显示提示二维码无效。
func found(code: String) {
print(code)
// convert JSON to object
do {
let qrMessage = try JSONDecoder().decode(QrMessage.self, from: code.data(using: .utf8)!)
print("QrMessage object - device name: " + qrMessage.pcName)
}
catch let error {
print(error)
_ = navigationController?.popViewController(animated: true)
// call ShowInvalidQrToast() from the new VC that is now on top of the ViewController stack
}
编辑: 我设法从该线程的第一个答案中找到使用 NotificationCenter 的解决方案:Calling function from another ViewController in swift。我在下面的回答中有更多信息。如果您认为有更好的方法,请post您自己的答案。
我的解决方案使用NotificationCenter:
在ConnectViewController中
override func viewDidLoad() {
super.viewDidLoad()
...
NotificationCenter.default.addObserver(self, selector: #selector(showInvalidQrCodeToast(_:)), name: Notification.Name(rawValue: "showInvalidQrCodeToast"), object: nil)
}
@objc func showInvalidQrCodeToast(_ notification: Notification) {
self.view.makeToast("Invalid QR code")
}
在ScannerViewController中
func found(code: String) {
print(code)
// convert JSON to object
do {
let qrMessage = try JSONDecoder().decode(QrMessage.self, from: code.data(using: .utf8)!)
print("QrMessage object - device name: " + qrMessage.pcName)
}
catch let error {
print(error)
_ = navigationController?.popViewController(animated: true)
NotificationCenter.default.post(name: Notification.Name(rawValue: "showInvalidQrCodeToast"), object: nil)
}