gpt4 book ai didi

ios - swift performSegueWithIdentifier 不工作

转载 作者:IT王子 更新时间:2023-10-29 05:02:02 34 4
gpt4 key购买 nike

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

   @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")
}
}

}

我将 Storyboard ID 设置为“测试”,当输入正确的信息时它不会切换 View Controller 。有人可以帮我解决我的问题吗?

Here is the code for the LoginViewController Here is the attributes panel for the KlikurTableViewController

最佳答案

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

至少有一个问题是:

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

应该是:

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

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

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

附录

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

附录 2

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

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

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

NSOperation vs. Grand Central Dispatch (GCD)

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

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 在这里隐藏了单线程规则:

NOTE

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

关于ios - swift performSegueWithIdentifier 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32292600/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com