Following is the storyboard of my app:
The app establishes a connection with the server on screen1 and all the communication with the server is performed in the code of this screen only. We make a request on screen4 and send it to server through the code of screen1 and app receives the response from the sever. If app gets successful response then app should show screen5, where I get error. I wrote different lines of code but failed. Following are line of code which I am using now:
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
}
}
}
I got the following error:
Warning: Attempt to present <project.screen5: 0x7a094790> on <project.ViewController: 0x79639d90> whose view is not in the window hierarchy!
screenshot of error:
Your problem is that view of the ViewController 1 is not in the window hierarchy, thus ViewController 1 cannot present modal VC.
Cleanest fix would be to change your app architecture design - having one view controller perform all the network requests may cause more complications than just this one.
However, for 'just make it work' solution you can present modal VC from navigation controller, i.e.