我正在尝试制作IOS应用程序并检查用户是否已登录。当用户未登录时,它应显示登录表单。我创建了多个视图控制器并将它们与segue连接起来。比我使用self.performSegueWithIdentifier("userLoggedOff", sender: self)
来激活segue。但是出现错误EXC_BAD_ACCESS
。
但是,当我尝试使用链接到按钮的相同代码激活相同的segue时,一切正常。非常令人沮丧... Thnx的帮助。
这是完整的代码。我使用JSON来获取用户的帐户信息。它可能不是这样做的方式,但我是xcode,swift和app-making的初学者。
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)
}
}
这是错误: Screenshot of the error
以下是完整输出:
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>
答案 0 :(得分:0)
@ Paulw11在我的帖子评论中解决了这个问题。
dispatch_async(dispatch_get_main_queue(), { self.performSegue...
感谢大家的帮助!
答案 1 :(得分:-1)
我认为你可能需要实现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.
}
}