viewDidLoad没有在UITabViewController上调用项目

时间:2015-11-25 17:19:39

标签: ios swift swift2

单击按钮

,通过segue,流程转到UITabViewController。

            self.performSegueWithIdentifier(self.gotoResult, sender: nil)
   let myUrl = NSURL(string: "XXXXXXXX");
        let request = NSMutableURLRequest(URL:myUrl!);
        request.HTTPMethod = "POST";
        // Compose a query string

        resultVar.city = cityText.text
        resultVar.state = streetText.text

        let postString = "streetaddr=\(streetText.text)&city=\(cityText.text)&state=\(stateVal)&degree=\(degreeVal)";

        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in

            if error != nil
            {
                print("error= \(error)")
                return
            }
            // You can print out response object
            print("response = \(response)")

            // Print out response body
            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")


            do {
                    resultVar.myJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
                } catch let error2 as NSError? {
                        print("error 2 \(error2)")
            }

        }

            task.resume()

有一个包含三个项目的UITabViewController。当初始视图被加载(item1)时,不会调用viewDidLoad。现在我在viewDidAppear中添加了相同的代码,当我单击另一个选项卡并返回到item1时,将填充字段。但我希望它只在segue之后的初始加载。我错过了什么?

Item1的ViewdidLoad

  override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        print("in here CityViewController")

        if let parseJSON = resultVar.myJSON {
            // Now we can access value of elements by its key
            var weather_condition = parseJSON["weather_condition"] as! String
            print("weather_condition: \(weather_condition)")

            weatherconditionLbl.text = weather_condition
        }

切换标签时调用的代码:

 override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)


        print("in here CityViewController1")

        if let parseJSON = resultVar.myJSON {
            // Now we can access value of elements by its key
            var weather_condition = parseJSON["weather_condition"] as! String
            print("weather_condition: \(weather_condition)")


            weatherconditionLbl.text = weather_condition + "in " + resultVar.city+","+resultVar.state


        }

    }

因此,当我切换标签时,不会调用代码viewdidload并调用viewDidAppear。

1 个答案:

答案 0 :(得分:0)

网络请求将比渲染下一个场景慢,因此不存在“parseJSON”。您需要使用网络请求的回调来刷新图形。为了做到这一点,我建议您每次需要刷新其内容时由“项目1”调用网络请求(由您自己决定)。