This may sound a newbie question, however I’m new to iOS dev.
Suppose with have this code.
UILabel* label = [[UILabel alloc] init];
...
[someScrollView addSubview:label];
...
label.text = @"Some Text";
is it good practice to modify the view after addSubview ?
Actually my concern is following probably, it is possible that label get released before reaching to label.text assignment, for instance in viewDidUnload, right ? and the assignment will fail.
Overall my questions are
- it is good practice to modify views after addSubview ?
- is it good practice to release view after addSubview, and later if I need to get any subview to look for it using following technique
for (UIView *view in self.subviews) { if (...) ... }?
Your code is fine as long as it is all in the same method and
labelis not reassigned during any of the … sections.Modifying a view before or after adding it to a subview makes no difference.
If you have allocated a view, then added it to a subview, and you don’t wish to keep a separate reference to it, you should release it – this is standard memory management. The super view will retain its subviews.
To get hold of a reference to your subview again, your two options are:
viewWithTag:to get it later