-2

以下是我的应用程序的情节提要: 在此处输入图像描述

该应用程序在 screen1 上与服务器建立连接,并且与服务器的所有通信仅在此屏幕的代码中执行。我们在 screen4 上发出请求,通过 screen1 的代码发送到服务器,app 收到服务器的响应。如果应用程序获得成功响应,那么应用程序应该显示 screen5,我得到错误。我写了不同的代码行但失败了。以下是我现在使用的代码行:

import UIKit

class logoViewController: UIViewController {

    @IBOutlet weak var act: UIActivityIndicatorView!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.act.startAnimating()



        // Do any additional setup after loading the view.
    }


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

    // syncreq function is called from the connectionViewController.
    // connectionViewController is the common class for connecting to the remote server  
    func syncreq (JSONdata: AnyObject) { // Proceesing for PRMS response


       // Getting the value from the JSON
        var Successful = self.getIntFromJSON(JSONdata as NSDictionary, key: "Successful")


        println("Value of Successful : \(Successful)")

        if (Successful == 0){

            //Method1 not worked
  // let adduser = regVC()
  // self.presentViewController(adducer, animated: true, completion: nil)
   //Method2 not worked
 //let adducer =    self.storyboard?.instantiateViewControllerWithIdentifier("registrationID") as regVC

        //self.navigationController?.pushViewController(adducer, animated: true)

           //Method3 not worked
            //let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("registrationID") as regVC

            //self.navigationController?.pushViewController(secondViewController, animated: true)
            //performSegueWithIdentifier("registrationID", sender: self)


            let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("registrationID") as RegisterViewController
            self.presentViewController(setViewController, animated: false, completion: nil)

        }
        else if (Successful == 1){
             let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("mnuID") as menuViewController
            self.presentViewController(setViewController, animated: false, completion: nil)
        }



    }

    func getIntFromJSON(data: NSDictionary, key: String) -> Int {

        let info : AnyObject? = data[key]
        // println("Value of data[key] : \(key)")

        if let info = data[key] as? Int {
            println("Value of value for \(key) : \(info)")
            return info
        }

        else {
            return 0

        }

    }

}

我收到以下错误:

Warning: Attempt to present <project.screen5: 0x7a094790> on <project.ViewController: 0x79639d90> whose view is not in the window hierarchy!

错误截图: 在此处输入图像描述

4

1 回答 1

0

您的问题是 ViewController 1 的视图不在窗口层次结构中,因此 ViewController 1 无法呈现模态VC。

最干净的解决方法是改变你的应用架构设计——让一个视图控制器执行所有的网络请求可能会导致比这个更复杂的问题。

但是,对于“让它工作”解决方案,您可以从导航控制器呈现模态 VC,即

self.navigationController?.presentViewController(setViewController, animated: false, completion: nil)
于 2015-03-24T08:11:29.667 回答