gpt4 book ai didi

ios - 在 IOS 的 webservice 中加载数据

转载 作者:可可西里 更新时间:2023-11-01 01:36:47 24 4
gpt4 key购买 nike

我是 IOS 的新手,我在做一个小项目。在我的项目中,我必须从主 Controller 中的 Web 服务 (SOAP) 加载数据 -> 将数据放入 seacond Controller 。因此,也许数据加载在后台线程中,该方法将数据在加载方法之前运行。那么我该如何解决呢?这是我的代码:

class MainRevealController: SWRevealViewController {
var conectData:ConnectData = ConnectData()
var menuTitle:[String] = []
var menuIcon:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
self.conectData.DemoSVNew("", key: "", commandType: "Menu", version: "") { (stringData) in
print("Hello I'm Loading Data")
let dataArray = stringData.componentsSeparatedByString("|")
let decodedData = NSData(base64EncodedString: dataArray[2], options: NSDataBase64DecodingOptions(rawValue: 0))
let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)
let dataArr = decodedString!.componentsSeparatedByString("<row>")
for i in 0...dataArr.count-1 {
let menu = dataArr[i].componentsSeparatedByString("|")
self.menuTitle.append(menu[3])
self.menuIcon.append(menu[4])
}
print("LoadData :",self.menuIcon.count)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "sw_rear" {
let mSegue = segue.destinationViewController as! MenuController
mSegue.menuTitle = menuTitle
mSegue.menuIcon = menuIcon
print("icon :",self.menuIcon.count)
print("title :",self.menuIcon.count)
}
}

这是代码展示: enter image description here

最佳答案

MainRevealController 中进行以下更改。

class MainRevealController: SWRevealViewController {
override func viewDidLoad() {
super.viewDidLoad()
// *** Removed your API call from here as before it returns result you are on `MenuController` ***
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "sw_rear" {
let mSegue = segue.destinationViewController as! MenuController
// *** No need to pass data here, as your data is loaded in `MenuController` itself ***
}
}
}

以及 MenuController 中的后续更改。

class MenuController {

var conectData:ConnectData = ConnectData()
var menuTitle:[String] = []
var menuIcon:[String] = []

override func viewDidLoad() {
super.viewDidLoad()

// *** Call your API here for menu items and perform action on result ***
self.conectData.DemoSVNew("", key: "", commandType: "Menu", version: "") { (stringData) in
print("Hello I'm Loading Data")
let dataArray = stringData.componentsSeparatedByString("|")
let decodedData = NSData(base64EncodedString: dataArray[2], options: NSDataBase64DecodingOptions(rawValue: 0))
let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding)
let dataArr = decodedString!.componentsSeparatedByString("<row>")
for i in 0...dataArr.count-1 {
let menu = dataArr[i].componentsSeparatedByString("|")
self.menuTitle.append(menu[3])
self.menuIcon.append(menu[4])
}
print("LoadData :",self.menuIcon.count)

// *** Do somethign with your API result ***
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

我希望你现在清楚了。快乐编码:)

关于ios - 在 IOS 的 webservice 中加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36394330/

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