Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone: Create a reusable component (control) that has some Interface Builder pieces and some code
    text
    copied!<p>I want to create a reusable component (a custom control) for the iPhone. It consists of several standard controls prearranged on a View, and then some associated code. My goals are:</p> <ol> <li>I want to be able to use Interface Builder to lay out the subviews in my custom control;</li> <li>I want to somehow package the whole thing up so that I can then fairly easily drag and drop the resulting custom component into other Views, without having to manually rewire a bunch of outlets and so on. (A little manual rewiring is fine, I just don't want to do tons and tons of it.)</li> </ol> <p>Let me be more concrete, and tell you specifically what my control is supposed to do. In my app, I sometimes need to hit a web service to validate data that the user has entered. While waiting for a reply from the web service, I want to display a spinner (an activity indicator). If the web services replies with a success code, I want to display a "success" checkmark. If the web service replies with an error code, I want to display an error icon and an error message.</p> <p>The single-use way to do this is pretty easy: I just create a UIView that contains a UIActivityIndicatorView, two UIImages (one for the success icon and one for the error icon), and a UILabel for the error message. Here's a screenshot, with the relevant parts marked in red:</p> <p><img src="https://i.stack.imgur.com/oMHsv.png" alt="alt text"></p> <p>I then wire up the pieces to outlets, and I put some code in my controller.</p> <p>But how do I package up those pieces -- the code and the little collection of views -- so that I can reuse them? Here are a few things I found that get me partway there, but aren't that great:</p> <ul> <li>I can drag the collection of views and controls into the Custom Objects section of the Library; then, later, I can drag them back out onto other views. But (a) it forgets which images were associated with the two UIImages, (b) there is a lot of manual rewiring of four or five outlets, and (c) most importantly, this doesn't do bring along the code. (Perhaps there's an easy way to wire up the code?)</li> <li>I think I could create an IBPlugin; not sure if that would help, and it seems like a lot of work, and also it's not entirely clear to me whether IBPlugins work for iPhone development.</li> <li>I thought, "Hmm, there's code associated with this -- that smells like a controller," so I tried creating a custom controller (e.g. <code>WebServiceValidatorController</code>) with associated XIB file. That actually feels really promising, but then at that point I can't figure out how, in Interface Builder, to drag this component onto other views. The <code>WebServiceValidatorController</code> is a controller, not a view, so I can drag it into a Document Window, but not into a view.</li> </ul> <p>I have a feeling I'm missing something obvious...</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