[英]Segue won't perform when activated without a button xcode Swift 2
I'm trying to make an IOS application and checking whether the user has logged in or not.我正在尝试制作一个 IOS 应用程序并检查用户是否已登录。 When a user is not logged in, it should display a login form.当用户没有登录时,它应该显示一个登录表单。 Ive created multiple View Controllers and connected them with segue's.我创建了多个视图控制器并将它们与 segue 连接起来。 Than I used self.performSegueWithIdentifier("userLoggedOff", sender: self)
to activate the segue.比我使用self.performSegueWithIdentifier("userLoggedOff", sender: self)
来激活 segue。 But the error EXC_BAD_ACCESS
appears.但是出现错误EXC_BAD_ACCESS
。
However, when I try to activate the same segue with the same code linked to a button, it all works.但是,当我尝试使用链接到按钮的相同代码激活相同的 segue 时,一切正常。 Really frustrating... Thnx for your help.真的很令人沮丧......感谢您的帮助。
This is the full code.这是完整的代码。 I used JSON to get the user's account info.我使用 JSON 来获取用户的帐户信息。 It's probably not the way to do it, but i'm a beginner in xcode, swift and app-making in general.这可能不是这样做的方法,但我是 xcode、swift 和应用程序制作的初学者。
import UIKit
import WebKit
class StartController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
checkLoginStatus_main()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func checkLoginStatus_main(){
let requestURL: NSURL = NSURL(string: "https://www.example.net/account/accountinfo.php")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
if let accounts = json["account"] as? [[String: AnyObject]] {
for account in accounts {
if let status = account["status"] as? String {
if let name = account["name"] as? String {
if(status == "true"){
print("LoggedIn") // THIS WORKS
self.continueScanner()
} else {
print("LoggedOut") // THIS WORKS
self.continueLoginForm()
}
}
}
}
}
}catch {
print("Error with Json: \(error)")
}
}
}
task.resume()
}
func continueLoginForm(){
self.performSegueWithIdentifier("userLoggedOff", sender: self)
}
func continueScanner(){
self.performSegueWithIdentifier("userLoggedIn", sender: self)
}
}
This is the error: Screenshot of the error这是错误:错误的屏幕截图
Here is the full output:这是完整的输出:
LoggedOut
1 0x18771c654 <redacted>
2 0x1834bcfe4 <redacted>
3 0x1834b1e50 _os_once
4 0x1834ba728 pthread_once
5 0x1886a6d98 <redacted>
6 0x1886a6680 WebKitInitialize
7 0x188c4bfa0 <redacted>
8 0x100791a3c _dispatch_client_callout
9 0x1007928b4 dispatch_once_f
10 0x182ebcfc8 <redacted>
11 0x182ec38b8 <redacted>
12 0x182ecdd78 <redacted>
13 0x188f9c130 <redacted>
14 0x188f9c2cc <redacted>
15 0x188f9be9c <redacted>
16 0x188ca9ff4 <redacted>
17 0x188f9c154 <redacted>
18 0x188f9be9c <redacted>
19 0x188e780a0 <redacted>
20 0x188f9c154 <redacted>
21 0x188f9c2cc <redacted>
22 0x188f9be9c <redacted>
23 0x188e773d4 <redacted>
24 0x188d19140 <redacted>
25 0x188addfcc <redacted>
26 0x1889a07ec <redacted>
27 0x1889a0744 <redacted>
28 0x18928f504 <redacted>
29 0x188cfcca0 <redacted>
30 0x188d22a04 <redacted>
31 0x188d257a0 <redacted>
@Paulw11 has solved the problem in the comments on my post. @Paulw11 已经解决了我帖子评论中的问题。
dispatch_async(dispatch_get_main_queue(), { self.performSegue...
Thanks for your help everyone!谢谢大家的帮助!
I think you probably need to implement prepareForSegue我认为您可能需要实施 prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "userLoggedOff") {
let vc = segue.destinationViewController as? LogInScreenViewController
} else if (segue.identifier == "userLoggedIn") {
let vc = segue.destinationViewController as? UserLoggedInViewController
//This should be the name of whatever view controller they go to if they are logged in.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.