Obj C: Calling an action just once
Hey everbody, im getting some trouble here. When my viewcontroller reaches the view, it sends to php some data. BUT...i've looking for, and discovered viewDidLoad gets called twice.
So, how can 开发者_StackOverflow社区i call some action just once?
I know this is a noob question, but i wanna know the besto method.
Thanks!
It shouldn't be getting called twice. You really want to look into why this is happening.
I had this happen in a UIView once before. I don't remember the exact reason, but it turned out in the end - I actually had two copies of my UIview running - one on top of another - the lower one being invisible.
You can determine this by looking at the value of "self" in the debugger when viewDidLoad is called - if it is different - that means you have two different instances being loaded.
The other suggestion about setting a private "bool" value won't work - because each of these instantiated UIViews will each have their own private copy of the bool!
I can't remember why this was happening for me - but I do also remember seeing someone on StackOverflow reporting the same issue. It either had something to do with the appDelegate creating the object twice, or two copies of the view in the XIB - I can't remember - but I do remember it only took a few minutes of poking around to figure out what was going on.
You know it's gone when you set the breakpoint in viewDidLoad and it only gets hit once.
If you really get really caught - you could always do the blasphemous thing - and initialize a global BOOL to "false", and do the check and set thing with it - but this will only hide the real problem at-hand.
In your viewController create a private BOOL
value that indicates whether that request has been made or not, and if it hasn't, get the data, and set that BOOL
value to YES
.
精彩评论