在IOS中的Web服务中加载数据

时间:2016-04-04 03:55:29

标签: ios swift web-services asynchronous

我是IOS的新手,我在小项目中。在我的项目中,我必须从主控制器中的Webservice(SOAP)加载数据 - >将数据放入seacond控制器。 所以,也许数据在后台线程中加载,该方法在load方法之前运行数据。那我该如何解决呢? 这是我的代码:

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

1 个答案:

答案 0 :(得分:2)

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.
    }
}

我希望你现在明白了。快乐的编码:)