以下是我的应用的故事板:
应用程序与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!
错误的屏幕截图:
答案 0 :(得分:0)
您的问题是ViewController 1的视图不在窗口层次结构中,因此ViewController 1无法呈现模态VC。
最干净的修复方法是更改您的应用程序架构设计 - 让一个视图控制器执行所有网络请求可能会导致更多复杂问题。
然而,为了让它发挥作用&#39;解决方案,您可以从导航控制器提供模态VC,即
self.navigationController?.presentViewController(setViewController, animated: false, completion: nil)