我正在使用if语句来运行segue。我有if语句在成功运行时打印一条消息。但是,segue没有运行,我不知道为什么。
override func viewDidLoad() {
super.viewDidLoad()
firstField.delegate = self
signUpButtonOutlet.isEnabled = false
// Do any additional setup after loading the view.
let fetchRequest: NSFetchRequest<Check> = Check.fetchRequest()
do {
//go get the results
let searchResults = try getContext().fetch(fetchRequest)
//I like to check the size of the returned results!
print ("num of results = \(searchResults.count)")
//You need to convert to NSManagedObject to use 'for' loops
for check in searchResults as [NSManagedObject] {
//get the Key Value pairs (although there may be a better way to do that...
print("the value was\(check.value(forKey: "isLoggedIn"))")
if (check.value(forKey: "isLoggedIn") != nil) {
self.performSegue(withIdentifier: "loggedInTrue", sender: self)
print("if statement ran")
}
}
} catch {
print("Error with request: \(error)")
}
}
答案 0 :(得分:1)
将登录状态存储在viewDidLoad
中,并在viewDidAppear
中执行搜索。如果登录状态为true,则会隐藏第一个ViewController。
override func viewWillAppear(_ animated: Bool) {
if (isloggedIn != false) {
self.view.isHidden = true
} else {
self.view.isHidden = false
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
if (isloggedIn != false) {
self.performSegue(withIdentifier: "loggedInTrue", sender: self)
}
}
或强>
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("loggedInTrue", sender: self)
}
答案 1 :(得分:1)
最好的办法是在检查用户是否已登录后,从appDelegate加载“loggedIn”控制器。
因此,例如在app委托的didFinishLaunchingWithOptions方法中检查用户默认情况,以查看用户是否登录
if UserDefaults.standard().bool(forKey: "logged_in") == true {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "YOURSTORYBOARDNAME", bundle: nil)
let loggedIn = storyboard.instantiateViewController(withIdentifier: "LOGGEDIN") as! UIViewController
self.window?.rootViewController = loggedIn
}
显然,要使用此功能,您需要在用户实际登录时将loggedIn变量设置为true。就像这样
UserDefaults.standard().setBool(true, forKey: "logged_in")
您必须注意的一件事是,将YOURSTORYBOARDNAME更改为您的实际故事板,您还必须在故事板中设置该控制器的标识符