Hei,下面的代码不知道如何与segue合作。它返回:
警告:尝试显示其视图不在窗口层次结构中!
代码在IBAction按钮中运行。然后我执行没有PFFacebookUtils代码的segue,它工作正常。从那以后我猜想不能从PFFacebookUtils调用segue,因为它在某种程度上在后台工作。
代码:
PFFacebookUtils.logInInBackground(withReadPermissions: permissions) {
(user: PFUser?, error: Error?) -> Void in
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!")
self.performSegue(withIdentifier: "segueSignUpWindow", sender: nil)
} else {
print("User logged in through Facebook!")
self.performSegue(withIdentifier: "segueSignUpWindow", sender: self)
} else {
print("Uh oh. The user cancelled the Facebook login.")
self.loginCancelledLabel.alpha = 1
}
}
我尝试插入其他代码,例如:
DispatchQueue.main.async {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SignUp")
self.show(vc, sender: self)
print("Hello")
或使用功能:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "segueSignUpWindow") {
DispatchQueue.main.async {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SignUp")
self.show(vc, sender: self)
print("Hello")
}
}
没有任何作用。有没有人有工作的解决方案?我的目标是在登录或登录后继续使用segue到另一个故事板。
我已经查看了stackoverflow中的大多数线程。没有找到可行的解决方案。
答案 0 :(得分:1)
我最接近的是:
使用初始化值触发 var segueLogin:Bool = false 。
当PFFacebookUtils获得segue所需的值时,将触发器更改为true:
PFFacebookUtils.logInInBackground(withReadPermissions: permissions) {
(user: PFUser?, error: Error?) -> Void in
if let user = user {
if user.isNew {
print("User signed up and logged in through Facebook!")
self.segueLogin = true
} else {
print("User logged in through Facebook!")
self.segueLogin = true
}
} else {
print("Uh oh. The user cancelled the Facebook login.")
self.loginCancelledLabel.alpha = 1
}
}
然后将代码添加到 viewDidAppear 类。实现它每次PFFacebookUtils完成时启动。因此它会检查返回值是否为true并在成功执行PFFacebookUtils会话后执行segue:
override func viewDidAppear(_ animated: Bool) {
if segueLogin == true {
self.performSegue(withIdentifier: "segueSingup", sender: self)
}
}