Swift performSegueWithIdentifier 不起作用 [英] Swift performSegueWithIdentifier not working

查看:22
本文介绍了Swift performSegueWithIdentifier 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户成功登录其帐户后切换视图控制器,但它无法正常工作.我不能直接使用 segue,因为如果单击登录按钮,无论信息是否正确,它都会转到该视图控制器.我已经尝试了我所知道的一切,但都没有成功.这是我正在尝试的代码.

I am trying to switch view controllers after a user successfully logs in to their account, but it is not working correctly. I cant use a segue directly because if the login button is clicked it will go to that view controller regardless if the information is correct or not. I have tried everything that I know of with no success. This is the code I am trying.

   @IBAction func loginTapped(sender: AnyObject) {

    let username = usernameField.text
    let password = passwordField.text

    if username.isEmpty || password.isEmpty {
        var emptyFieldsError:UIAlertView = UIAlertView(title: "Please try again", message: "Please fill in all the fields we can get you logged in to your account.", delegate: self, cancelButtonTitle: "Try again")
        emptyFieldsError.show()
    }

    PFUser.logInWithUsernameInBackground(username, password:password) {
        (user: PFUser?, error: NSError?) -> Void in
        if user != nil {
            self.performSegueWithIdentifier("Klikur", sender: self)
        } else {
            if let errorString = error!.userInfo?["error"] as? String {
                self.errorMessage = errorString
            }

            self.alertView("Please try again", message: "The username password combiation you have given us does not match our records, please try again.", buttonName: "Try again")
        }
    }

}

我将情节提要 ID 设置为测试",并且在输入正确信息时它不会切换视图控制器.有人可以帮我解决我的问题吗?

I have the storyboard ID set to "Test" and it is not switching view controller when the correct information is entered. Can somebody help me resolve my problem?

推荐答案

[假设您的代码没有崩溃,而只是无法继续]

[Assuming that your code is not crashing, but rather just failing to segue]

至少有一个问题是:

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

应该是:

dispatch_async(dispatch_get_main_queue()) {
    [unowned self] in
    self.performSegueWithIdentifier("Test", sender: self)
}

请记住,所有 UI 操作都必须在主线程的队列上执行.您可以通过检查来向自己证明您在错误的线程上:

Remember that all UI operations must be performed on the main thread's queue. You can prove to yourself you're on the wrong thread by checking:

NSThread.isMainThread() // is going to be false in the PF completion handler

附录

如果有任何机会 self 可能变为 nil,例如因为不需要而被解雇或以其他方式解除分配,您应该将 self 弱捕获为 [weak self] 而不是 unowned,并使用安全解包:if let s = self { s.doStuff() } 或可选链:self?.doStuff(...)

If there's any chance self might become nil, such as getting dismissed or otherwise deallocated because it's not needed, you should capture self weakly as [weak self] not unowned, and use safe unwrapping: if let s = self { s.doStuff() } or optional chaining: self?.doStuff(...)

附录 2

这似乎是一个流行的答案,因此在这里提及这个较新的替代方案很重要:

This seems to be a popular answer so it's important to mention this newer alternative here:

NSOperationQueue.mainQueue().addOperationWithBlock {
     [weak self] in
     self?.performSegueWithIdentifier("Test", sender: self)
}

注意,来自 https://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift:

NSOperation 与 Grand Central Dispatch (GCD)

NSOperation vs. Grand Central Dispatch (GCD)

GCD [dispatch_* calls] 是一种轻量级的方式来表示将要并发执行的工作单元.

GCD [dispatch_* calls] is a lightweight way to represent units of work that are going to be executed concurrently.

NSOperation 与 GCD 相比增加了一点额外的开销,但您可以在各种操作之间添加依赖并重用、取消或暂停它们.

NSOperation adds a little extra overhead compared to GCD, but you can add dependency among various operations and re-use, cancel or suspend them.

附录 3

Apple 在这里隐藏了单线程规则:

Apple hides the single-threaded rule here:

注意

在大多数情况下,仅在应用程序的主线程中使用 UIKit 类.对于从 UIResponder 派生的类尤其如此涉及以任何方式操纵应用的用户界面.

For the most part, use UIKit classes only from your app’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your app’s user interface in any way.

SWIFT 4

DispatchQueue.main.async(){
   self.performSegue(withIdentifier: "Test", sender: self)
}

参考:

https://developer.apple.com/documentation/uikit

这篇关于Swift performSegueWithIdentifier 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆