IOS creates reusable views

I am trying to create my own view using nib files and the accompanying .h / .m files, and then create several instances of this view through IB and use them as subzones of another view.

  • I created .h / .m files.
  • I created xib.
  • For xib, I specify the class name in IB.
  • Add some labels to xib (values ​​for which will be programmatically changed)
  • I drag and drop a simple storyboard view into my container.
  • I set the drag-and-drop view class to class i specified for xib.

I do steps 5 and 6 several times, once for each view I want. In the end, I would associate these views with IBOutlets in the parent view class.

Obviously, I am doing something wrong, because I do not see my tags in the user view. I suspect that I need to associate either with the view directly, as well as with the collection / table view, but I did not find where to do it.

What is the right way to do this? I suppose I could add views programmatically, but how can I handle the layout for different devices (e.g. iphone 4 vs 5)?

+4
source share
2 answers

What I describe below works

1) Create xib and .h / .m files for your custom view.
1a) Assuming that you need IBOutlets to view the items you want to manage, give the File Owner the class name specified in your .h file in the identity inspector.

2) In the .h file, define the property

@property (nonatomic, retain) IBOutlet UIView *contentView;

2a) define exit points for all the routines in your xib that you want programmatic access to.

3) In the .m file, synthesize the property and make

 - (void)awakeFromNib { NSLog(@"awake from nib"); [[NSBundle mainBundle] loadNibNamed:@"yourNibName" owner:self options:nil]; [self addSubview:self.contentView]; } 

4) Drag empty views from the palette to their container in the storyboard. Change the class of these views to the class name defined in your .h file.

When you launch the application, you should see the xib contents in your subzones.

5) Now you can define the output in your custom subview instances in the .h file in the container view and include them, as usual, in the storyboard.

+7
source

You cannot create a view in xib and use it in a storyboard. If you have already created it in an xib file, you can copy and paste it into your storyboard. If you want this to be the root view of the controller, delete the view you get with the controller and paste it into xib. You can then change the class of these views to your own class.

I’m not sure what you mean by “children of another view” - views do not have a parent-child relationship, this is a view-subview. Also, you don’t usually (ever?) Drag a view into the container view. There is a view controller in the container view, which you automatically get when you add the container view to another view. This controller is a child of the controller whose appearance is in the form of a container. If you want your custom view to appear in this container view, you would add that view to the view of the child controller (or replace this view with yours).

+2
source

All Articles