@IBAction func DoneButton(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as UIViewController
viewController.viewWillAppear()
}
Its suppose to dismiss my current view and reload ViewController
but it crashes with fatal error: unexpectedly
found nil while unwrapping an Optional value
is it because I'm dismissing before?
The call to instantiateViewControllerWithIdentifier()
returns an optional. So if the view controller with the specified identifier cannot be found in your storyboard, it will be set to nil and the next line where you call viewWillAppear
will crash.
It is also possible that you crash inside viewWillAppear
actually. That is not a method that you should call. Instead, UIKit will call that method for you when your view controller is presented.
I guess what you are trying to do is something like this:
if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as UIViewController {
self.presentViewController(viewController,
animated: true, completion: nil)
}
If not, provide more context to your question.
I was thinking to do either
1.
let viewController = ViewController()
viewController.viewWillAppear(false)
2.
In secondary class:
let viewController = ViewController()
viewController.shouldRefresh = true
In primary class:
var shouldRefresh: Bool!
override func viewWillAppear(animated: Bool) {
if shouldRefresh == true {
}
}
3.
In secondary class:
let viewController = ViewController()
viewController.reloadRoutineData()
In primary class:
func reloadRoutineData() {
// Do my stuff here
}
The third option is my variable because I only need to do certain code, not really reload the view completely. But all of them crash with nil while unwrapping.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.