gpt4 book ai didi

ios - popToViewController 首先执行

转载 作者:行者123 更新时间:2023-11-29 02:24:38 25 4
gpt4 key购买 nike

按下发布按钮时,将执行以下功能。在该函数中,使用 Parse 后端检索的所有对象都将附加到 groupConversation 数组中,该数组是一个全局数组。但是,当我引用在函数末尾弹出的 UITableViewController 中的数组并使用 println() 打印数组的内容时,该数组是空的。但是,当我在包含此函数的 UIViewController 中使用 println() 时,该数组显示为包含一个对象。在控制台中,按下按钮后弹出的 UITableViewControllerprintln() 在包含以下功能的 UIViewController。在弹出到 UITableViewController 之前,如何使下面的函数完全执行。

@IBAction func postButtonPressed(sender: AnyObject) {
//Adds Object To Key
var name=PFObject(className:currentScreen)
name["userPost"] = textView.text
name.saveInBackgroundWithBlock {
(success: Bool!, error: NSError!) -> Void in

if success == true {

self.textView.text=""


} else {

println("TypeMessageViewController Error")

}


}
//Gets all objects of the key
var messageDisplay = PFQuery(className:currentScreen)
messageDisplay.selectKeys(["userPost"])
messageDisplay.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil{
for object in objects {
var textObject = object["userPost"] as String
groupConversation.append(textObject)

}
} else {
// Log details of the failure

}
println("Type message \(groupConversation)")
}
navigationController!.popToViewController(navigationController!.viewControllers[1] as UIViewController, animated: true)
}

最佳答案

问题出在messageDisplay.findObjectsInBackgroundWithBlock。当您在后台线程中执行此操作时,它将与主线程分开。您的主线程将按应有的方式执行。

所以在完成任务之前,你主线程弹出 View 。

messageDisplay.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil{
for object in objects {
var textObject = object["userPost"] as String
groupConversation.append(textObject)

}
} else {
// Log details of the failure

}
println("Type message \(groupConversation)")
dispatch_async(dispatch_get_main_queue()) {
self.navigationController!.popToViewController(navigationController!.viewControllers[1] as UIViewController, animated: true)
return
}
}

在后台线程中推送和弹出可能会导致问题。所以在后台执行任务后获取主线程,然后在主线程中pop。

在 swift 单语句闭包中自动返回语句返回值。在您的特定情况下,它试图返回 [AnyObject]? 的实例,这是 popToViewControllerAnimated 的返回值。 dispatch_after 预期的关闭是 Void -> Void 。由于闭包返回类型不匹配,编译器会对此进行提示。

希望这会有所帮助.. ;)

关于ios - popToViewController 首先执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27699226/

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