gpt4 book ai didi

iOS popToRootViewControllerAnimated 无法处理委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-28 05:29:59 24 4
gpt4 key购买 nike

我已经实现了一个自定义委托(delegate)方法,如果一些数据从服务器到达,它应该弹出 Root View Controller 。通过单击子 Controller 中的后退按钮,第一种方法 back() 检查文本字段中是否进行了任何更改,并在发生某些更改时显示 UIActionSheet。

1.

func back() {
if modified {
let actionSheet = UIActionSheet(title: NSLocalizedString("SAVESETTINGS", comment: "Settings were changed. Do you want to save them?"), delegate: self, cancelButtonTitle: "No", destructiveButtonTitle: "Yes")
actionSheet.actionSheetStyle = .Default
actionSheet.showInView(self.view)
}
else {
self.navigationController?.popToRootViewControllerAnimated(true)
}
}

ActionSheet 委托(delegate)方法发送数据, Controller 等待应答
2.

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == 0 {
self.socket.send(someData, tag: someTag)
}
else {
self.modified = false
self.navigationController?.popToRootViewControllerAnimated(true)
}
}

委托(delegate)方法检查正确的答案是否在这里并且应该弹出 Root View Controller 。

3.

func ackReceived(tag: Int32) {
if tag == someTag {
self.navigationController?.popToRootViewControllerAnimated(true)
}
}

如果没有更改或在 UIActionSheet 中按下第二个按钮, Root View Controller 将在第一个和第二个方法中弹出,但它在 3d 方法中不起作用。

调用所有方法。我结束了

self.navigationController?.popToRootViewControllerAnimated(true)

我也检查了调试器,导航 Controller 不是nil

我希望你能帮我解决这个问题。非常感谢。

最佳答案

感谢 Wain 的提示,我找到了问题所在。 func ackReceived(tag: Int32) {} 未在主队列中调用。UI 作为 UIKit 方法不是线程安全的,应该在主队列中调用。

func ackReceived(tag: Int32) {
if tag == someTag {
dispatch_async(dispatch_get_main_queue(), {
self.navigationController?.popToRootViewControllerAnimated(true)
return
})
}
}

是调用 UI 方法的正确方法。

非常感谢您的帮助。

关于iOS popToRootViewControllerAnimated 无法处理委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29166920/

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