My viewDidLoad
in a view controller is called twice. Once by [UIViewController View]
and a second time by [UINib instanciateWithOwner:Options]
. Why is this happening? Can it be prevented?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
There are two possibilities, whereby this issue happened in my iOS device frequently.
Rule #1: Do not call any view related setup in [init] function, all view related setup must be done in viewDidLoad and viewWillAppear.
Rule #2: Check viewDidLoad and viewWillAppear, are they calling correct super function? For example viewDidLoad -> super viewDidLoad and so on.
Hope this helps.
In my case, I used self.view (once) in viewDidLoad while calling viewDidLoad in my unit tests. This resulted in two calls. However, when I replaced [testedViewController viewDidLoad] with [testedViewController view], the double call problem was gone.
You might have to check the object building mechanism. If there is only one nib file with reference to the controller, then this method should not be called multiple times. (unless if the object is getting rebuilt).
I think you might have to make your code within ViewDidLoad idemPotent. It is always better to make sure, that framework call back methods make this assumption.
Any code you put inside of
viewDidLoad
should be able to run multiple times with out any issues. If you have code that only needs to run once for your controller use-awakeFromNib
. The reason is because the view of the view controller can be unloaded and loaded multiple times. The code inside ofviewDidLoad
should only modify the UI to reflect the current state.Now that I got that out of the way, your particular issue looks to be a bug. See Ned's answer.
Is this the same problem?
Why is viewDidLoad called twice when the rootViewController property of UIWindow is set?
Looks like it might be a bug in XCode 4.