2016-07-11 5 views
-1

Ich versuche, von der Anmeldung zum View Menu Controller (id Storyboard: Menü) übergeben.Ändern Sie die Ansicht von Aufgabe zu anderen swift

XCODE STORYBOARD

Um View Controller schalte ich einen Übergang in den Login-Button haben, wird Login mit Login.swift verbunden.

In Login.swift, indem Sie auf die Schaltfläche Login klicken geschieht Folgendes:

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

In Antrag habe ich dies:

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 Anmeldung 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); 

} 

kommentiert Linien sind alle Wege, die ich versucht habe, zum View Menu Controller zu gehen.

Beim Versuch, mit:

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

bekomme ich diesen Fehler:

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

Aber die segue zwischen Anmeldung und Menü Ansicht Controller ist die Kennung goToMenu zugeordnet

Wenn mit dem Versuch, :

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

Nicht funktioniert oder schlägt fehl.

Beim Versuch, mit:

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

ich diesen Fehler:

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

Wie kann ich es tun?

+0

zwei Dinge vergessen Sie hier –

+1

Sie benötigen 'ViewController' mit' UINavugationController' einzubetten. –

Antwort

0

Um es zu beheben Ich sendete "Selbst" von @IBAction Login Request(). Login und übergeben Sie erneut den Absender os Request(). Login zu PassPostData und den Absender in sender.self.performSegueWithIdentifier mit 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) 
    } 
} 

UND:

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(); 
}} 
Verwandte Themen