【问题标题】:Change view from task to other swift将视图从任务更改为其他 swift
【发布时间】:2016-07-11 12:29:24
【问题描述】:

我正在尝试从登录传递到视图菜单控制器(ID 故事板:菜单)。

要切换视图控制器,我有一个登录按钮,登录与 Login.swift 相关联。

在 Login.swift 中,通过单击 LOGIN 按钮执行以下操作:

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

在请求中我有这个:

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

在登录 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);

}

注释行是我尝试进入 View Menu Controller 的所有方式。

尝试时:

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

我收到此错误:

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

但是登录和视图菜单控制器之间的segue被分配了标识符goToMenu

尝试时:

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

我收到此错误:

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

我该怎么做?

【问题讨论】:

  • 两件事你忘记了
  • 您需要将ViewController 嵌入UINavugationController

标签: ios iphone swift xcode segue


【解决方案1】:

为了解决这个问题,我从@IBAction login 发送了“self”到 Request().login 并再次将发送者 os Request().login 传递给 passPostData 并使用 sender.self.performSegueWithIdentifier 中的发送者和 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)
    }
}

与:

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

【讨论】:

    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多