我在转回我的主视图 Controller 时遇到异常错误。
当用户在应用程序中注册时,它会创建一个唯一的 CloudKit 记录。保存操作完成后,从 cloudKit saveRecord 闭包中广播 NSNotification,向用户显示警报。当警报被接受时,将执行到新 viewController 的 segue。
当返回到主视图 Controller 时,它再次运行 viewDidLoad 和 viewDidAppear,导致各种功能停止工作。我发现我的主视图 Controller 的原始实例仍在内存中并且可以访问。
我认为这个问题可能与线程和 performForSegue 被从主线程中调用有关,尽管我使用的是 GCD,但事实并非如此。
这是我的代码:
保存功能:
func saveRecord(newUser: CKRecord) {
database.saveRecord(newUser) { record, error in
if error != nil {
print(error)
Properties.registrationError = error
} else {
Properties.registrationSuccess = record
print(record)
}
NSNotificationCenter.defaultCenter().postNotificationName(Key.Registration, object: self)
}
}
收到保存记录通知时:
func registrationComplete() {
dispatch_async(dispatch_get_main_queue()) {
self.pleaseWait.alpha = 0
self.stopIndicator()
print("stop indicator")
if Properties.registrationError != nil {
self.displayAlert("Registration Failed", error: "\(Properties.registrationError!.localizedDescription)", performAction: .None)
} else {
self.displayAlert("Registration Completed", error: "Press OK to begin induction", performAction: .BeginInduction)
}
}
}
当按下确定时:
self.performSegueWithIdentifier("beginInduction", sender: self)
为了测试,当新 View 出现时,我会直接返回到主 Controller 。此时又运行了原来controller的viewDidLoad。任何帮助将不胜感激,谢谢!
最佳答案
当您调用 performSegueWithIdentifier
时,它会加载一个与“beginInduction”标识符相关联的新 View Controller ,而不是返回到您可能使用先前的 performSegueWithIdentifier
创建的现有实例>。您需要做的是使用 unwind segue。
此链接中有更多详细信息。
https://developer.apple.com/library/ios/technotes/tn2298/_index.html
或者,您可以使用 popToRootViewControllerAnimated
或 popToViewController
或 popViewController
或 dismissViewController
方法返回到之前的 View Controller
关于ios - 执行 segue 后 ViewDidLoad 再次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35777251/