-1

I'm trying to pass from login to View Menu Controller (id storyboard: menu).

XCODE STORYBOARD

To switch View Controller I have a segue into the login button, login is associated with Login.swift.

In Login.swift by clicking on the LOGIN button does the following:

Request().login(tag, email: email,  pass: pass)

In Request i have this:

class Request {

func login(tag : String, email : String, pass : String){
    let url = NSURL(string: "xxx");
    let request = NSMutableURLRequest(URL:url!)
    request.HTTPMethod = "POST";

    let postString = "tag="+tag+"&email="+email+"&password="+pass;
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    let session = NSURLSession.sharedSession();
    let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        let json = JSON(data: data!)
        Login().passPostData(json)
    });

    task.resume();
}}

In Login passPostData:

func passPostData(json: JSON){
    let total = json.count
    for i in 0..<total{
        let id = json[i]["id"].string!
        let username = json[i]["username"].string!
        let birthday = json[i]["birthday"].string!
        let location = json[i]["location"].string!
        let email = json[i]["email"].string!
        let photo = json[i]["photo"].string!

        print(id + " - " + username + " - " + birthday + " - " + location + " - " + email + " - " + photo)
    }

    SwiftLoading().hideLoading()

    //self.performSegueWithIdentifier("goToMenu", sender: self)

    //let menuController = self.storyboard?.instantiateViewControllerWithIdentifier("menu") as? ViewMenuController
    //self.navigationController?.pushViewController(menuController!, animated: true)

    //let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil);
    //let vc : UIViewController = storyboard.instantiateViewControllerWithIdentifier("menu") as UIViewController;
    //self.presentViewController(vc, animated: true, completion: nil);

}

Commented lines are all the ways I've tried to go to View Menu Controller.

When trying with:

self.performSegueWithIdentifier("goToMenu", sender: self)

I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<xxx.Login: 0x7f9d28545000>) has no segue with identifier 'goToMenu''

But the segue that is between Login and View Menu Controller is assigned the identifier goToMenu

When trying with:

let menuController = self.storyboard?.instantiateViewControllerWithIdentifier("menu") as? ViewMenuController
    self.navigationController?.pushViewController(menuController!, animated: true)

Don`t works or fails.

When trying with:

let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil);
    let vc : UIViewController = storyboard.instantiateViewControllerWithIdentifier("menu") as UIViewController;
    self.presentViewController(vc, animated: true, completion: nil);

I get this error:

Warning: Attempt to present <xxx.ViewMenuController: 0x7f9139ef46e0> on <xxx.Login: 0x7f9139d871c0> whose view is not in the window hierarchy!

How can I do it?

1 Answers1

0

To fix it I sent 'self' from @IBAction login to Request().login and pass again the sender os Request().login to passPostData and use the sender in sender.self.performSegueWithIdentifier with dispatch_async(dispatch_get_main_queue())

@IBAction func login(sender: UIButton) {
    let email : String = emailText.text!
    let pass : String = passText.text!
    let tag : String = "login"

    if(email != "" && pass != ""){
        Request().login(self, tag: tag, email: email,  pass: pass)

        SwiftLoading().showLoading()
    }

}

func passPostData(sender: AnyObject, json: JSON){
    let total = json.count
    for i in 0..<total{
        let id = json[i]["id"].string!
        let username = json[i]["username"].string!
        let birthday = json[i]["birthday"].string!
        let location = json[i]["location"].string!
        let email = json[i]["email"].string!
        let photo = json[i]["photo"].string!

        print(id + " - " + username + " - " + birthday + " - " + location + " - " + email + " - " + photo)
    }
    SwiftLoading().hideLoading()

    dispatch_async(dispatch_get_main_queue()) {
        sender.self.performSegueWithIdentifier("goToMenu", sender:  sender.self)
    }
}

AND:

class Request {

func login(sender: AnyObject, tag : String, email : String, pass : String){
    let url = NSURL(string: "xxx");
    let request = NSMutableURLRequest(URL:url!)
    request.HTTPMethod = "POST";

    let postString = "tag="+tag+"&email="+email+"&password="+pass;
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    let session = NSURLSession.sharedSession();
    let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        let json = JSON(data: data!)
        Login().passPostData(sender, json: json)
    });

    task.resume();
}}