Ios 使用PerformsgueWithIdentifier Swift 2.1时出错

Ios 使用PerformsgueWithIdentifier Swift 2.1时出错,ios,swift,swift2,segue,uistoryboardsegue,Ios,Swift,Swift2,Segue,Uistoryboardsegue,我试图使用“performsguewithidentifier”以编程方式创建一个Segue,但由于某些原因,它不起作用。我将发布我的代码,因为它非常简单,但它显示了一个奇怪的错误 这是我的密码: import UIKit class LoginViewController: UIViewController { @IBOutlet weak var login: UIButton! override func viewDidLoad() { super.viewDidLoad()

我试图使用“performsguewithidentifier”以编程方式创建一个Segue,但由于某些原因,它不起作用。我将发布我的代码,因为它非常简单,但它显示了一个奇怪的错误

这是我的密码:

import UIKit

class LoginViewController: UIViewController {

@IBOutlet weak var login: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func loginButtonPressed(sender: UIButton) {

    if (((user.text) != "") && ((password.text) != ""))
    {
        let request = NSMutableURLRequest(URL: NSURL(string: "http://website.com/login.php")!)
        request.HTTPMethod = "POST"
        let postString = "user="+user.text!+"&password="+password.text!

        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in

            if error != nil
            {
                print(error)
                return
            }

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!

            if (responseString=="Correct")
            {
                self.performSegueWithIdentifier("loginSegue", sender: sender)
            }
            else
            {

            }

        }
        task.resume()
    }
    else
    {

    }
}
我已经创建了一个从我的
LoginViewController
到ID为“loginSegue”的
NavigationController
的序列,但它仍然不工作

我的代码出了什么问题

这就是Xcode的错误:

2015-11-23 21:09:43.224 App[5511:3896125] *** Assertion failure in -       [UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.29.5/Keyboard/UIKeyboardTaskQueue.m:378
2015-11-23 21:09:43.226 App[5511:3896125] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'
*** First throw call stack:
(0x2232985b 0x33c9edff 0x22329731 0x230baddb 0x2648a5fd 0x2648aeaf 0x2648483b 0x26aa2321 0x265f2b23 0x267c44e7 0x267c5e55 0x267c81e5 0x267c8451 0x26556367 0x267cb6e7 0x267cb735 0x267dfb99 0x267cb65b 0x26a6e6b9 0x26b45919 0x26b45747 0x267bd87d 0x72fd0 0x71b0c 0x6e560 0x21b505b5 0x21b5fcd7 0x230e856d 0x23049fdf 0x2303c3cf 0x230ea82d 0x11e461b 0x11dcf53 0x11e5b0f 0x11e5961 0x34558e0d 0x345589fc)
libc++abi.dylib: terminating with uncaught exception of type NSException
对此,最“简单”的修复方法可能是将对
self.performsguewithidentifier(“loginSegue”,sender:sender)
的调用包装在主线程上的
dispatch\u async
中,但是,目标控制器的代码可能存在我们在这里无法看到的其他问题


问题很可能是
nsursession
delegateQueue
不是主队列,并且,
[UIKeyboardTaskQueue waituntillalltasksarefished]
正在该线程中的某个位置调用,但只能在主线程上调用。

NSURLSession的完成块在会话的委托队列上调用,该队列在后台线程上运行,除非您另有指定

您不应该从后台线程调用任何UI代码(几乎是整个UIKit)。有时有效,有时无效,有时崩溃。不要这样做

因此,正如Dan在回答中所说,除非您将这些代码发送到主线程,否则您无法在这些完成块中执行任何UI代码

更改后的代码可能如下所示:

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in
        if error != nil
        {
            print(error)
            return
        }
        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!

        if (responseString=="Correct")
        {
            //Use dispatch_async to do the performSegueWithIdentifier
            //on the main thread.
            dispatch_async(dispatch_get_main_queue())
            {
              self.performSegueWithIdentifier("loginSegue", sender: sender)
            }
        }
        else
        {
        }
    }

看起来相关代码没有发布。您可以发布
loginSegue
的目标控制器的代码吗?您正在尝试从后台线程操作UI元素。看起来你在后台做一些与键盘相关的事情,你确定是segue崩溃了吗?Xcode输出表示不同的内容。如何调用loginButtonPressed()操作?