[英]ViewDidLoad calling again after segue performed
回到主視圖控制器時遇到異常錯誤。
當用戶在應用程序中注冊時,它會創建一個唯一的CloudKit記錄。 保存操作完成后,會從cloudKit saveRecord閉包中廣播NSNotification,向用戶顯示警報。 接受警報后,將執行對新viewController的監視。
當回到主視圖控制器時,它將再次運行viewDidLoad&viewDidAppear,從而導致各種功能停止工作。 我發現我的主視圖控制器的原始實例仍在內存中並且可以訪問。
我相信問題可能與線程有關,盡管我使用的是GCD,但主線程已關閉了performForSegue,但事實並非如此。
這是我的代碼:
保存功能:
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)
}
}
收到saveRecord通知時:
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)
為了進行測試,我將在出現新視圖時直接回到主控制器。 此時,原始控制器的viewDidLoad再次運行。 任何幫助,將不勝感激,謝謝!
當您調用performSegueWithIdentifier
,它將加載與“ beginInduction”標識符關聯的新視圖控制器,並且不會返回到您可能使用以前的performSegueWithIdentifier
創建的現有實例。 您要做的是使用放松的搜尋。
更多詳細信息在此鏈接中。
https://developer.apple.com/library/ios/technotes/tn2298/_index.html
或者,您可以使用popToRootViewControllerAnimated
或popToViewController
或popViewController
或dismissViewController
方法返回到以前的視圖控制器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.