Note that there are some explanatory texts on larger screens.

plurals
  1. POSubclassed UIView with custom init - is this sane?
    text
    copied!<p>I've been casting around looking for best-practices and whatnot for creating custom, reusable view components. So far, it seems like a custom UIView subclass is the way to go.</p> <p>In this case, all I need is a fixed-size view with easy access to a label, and a convenience initializer would be great. I could tag the label and use a constant, but it seems unnecessary given this (boilerplate like UIKit importing removed) (with ARC):</p> <pre><code>// .h file @interface MyView : UIView @property (nonatomic, weak) IBOutlet UILabel *label; + (id)withText:(NSString *)text; @end // .m file @implementation MyView + (id)withText:(NSString *)text { MyView *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] objectAtIndex:0]; view.label.text = text; return view; } @end </code></pre> <p>(edit: changed to class method. my concerns are still unchanged, though it's a bit less dubious overall)</p> <p>My MyView.xib file's only top-level element (a UIView) has a custom class set to MyView, and the outlet is hooked up to the FileOwner.</p> <p>And... this works. Perfectly. It renders correctly everywhere I'm using it, no exceptions, no clipping, no funny business at all that I can see.</p> <p>So far. I'm more than a little concerned that:</p> <ol> <li>I haven't seen this technique anywhere yet. Of course, I've only been looking for a relatively short time (a couple hours, passively).</li> <li>I'm not doing any of the recommended overriding of initWithFrame: / Coder: etc.</li> <li>I'm only using this in an extremely-limited realm right now, so it is nowhere <em>near</em> well tested for situations like the view going out of view, the drawing performance is unknown, I haven't checked thoroughly for memory leaks, etc.</li> <li>I'm almost certain I'm missing something. Most likely I'll find it shortly while using it more, but I may not find all of them.</li> </ol> <p>Does someone with more UIView experience than I have mind lending their brain for a bit? Is this a good idea? A horrifying "omg wtf are you thinking" idea? Are there surprises waiting for me? Is there something I should read for how to create simple custom views like this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload