Sunday, December 20, 2009

iPhone image hiding

I wanted to display a little red "X" when someone didn't enter all of the fields, but was having a hard time using the .hidden label. I finally found a really good post that I'll link here, and I'll also summarize for you.

This works for UITextField and I'm sure other objects as well.

Hide an object

Here are the basics.
  1. In your .h file for your view. In the same section as @interface enter: IBOutlet UIImageView *imageBadInput;
  2. Just below the interface section enter: @property (nonatomic, retain) IBOutlet UIImageView *imageBadInput;
  3. Now move to the .m file for you view and after @implementation enter : @synthesize imageBadInput;
  4. In the body of this file enter the following to hide the object, and use the NO form to display it: imageBadInput.hidden = YES;
  5. Save these files (they will be grey in the list on the left if they are not saved).
  6. Open up Interface Builder with your nib (.xib) file. Ctrl-click and drag either the File Owner, or the ViewController (depends on how you are building your app).
  7. Drag the blue line over to the graphic you want, and drop it once highlighted.
  8. You now have the option to select the outlet you created in step 1
  9. If you have created several of them, they will all be available. Objects already connected have a "-" before them.
  10. Save the nib file, and build as normal.
If you want to hide these objects unless there is a problem, you can implement this in your views .m file to hide the objects when the view loads.

- (void)viewDidLoad {
imageBadInput.hidden = YES;
}

No comments: