2

I have a UIViewController with an UIScrollView in it.

Then I created a new UIView subclass with some properties that I want to programmatically add in this UIScrollView until my data ends.

Something that should look like this picture: embed uiview

I have read other topics that does that by creating a view and setting its class to the custom view subclass, but I want to do it programmatically and its not working.

how would I do that?

2
  • Why reinvent the wheel? Use a UITableView with custom UITableViewCells Commented Aug 6, 2012 at 22:53
  • Could be a possibility, but can I draw the cell just like I did in my view and make it work faster that in UIScrollView? Because the only difference I can see between using tableview ou scrollview is that I would not have to make the cellForRowAtIndexPath. Commented Aug 6, 2012 at 23:51

2 Answers 2

6

From your image it looks like you're looking to load views from a nib and add them as subviews of your UIScrollView. To do this have a look at the UINib documentation.

You want to create your nib and set it's main view to be an instance of your UIView subclass then load the nib in viewDidLoad of your viewController, and add the nib's views as subivews of your scrollview (which I'm assuming is a subview of your viewController's view).

You can instantiate a nib with instantiateWithOwner:options:.

This method unarchives each object, initializes it, sets its properties to their configured values, and reestablishes any connections to other objects

To get the array of views from a nib you do something similar to:

UINib *myNib = [UINib nibWithNibName:@"myNib" bundle:[NSBundle mainBundle]];
NSArray *viewsFromNib = [myNib instantiateWithOwner:nil options:nil];

I'll assume we're inside a UIViewController and we're somewhere in (or after) viewDidLoad. You would then use the array from above and add the views as subviews of your scrollview. You may need to set the frames of these views to place them properly, but that should be trivial.

UIView *aView = [viewsFromNib objectAtIndex:0];
[self.scrollView addSubview:aView];

I hope that sets you in the right direction.

Edit:

If you want more information you may need to read deeper into how nibs work to manage your expectation. Linked with the UINib documentation is the 'Resource Programming Guide' in particular the nib section

Sign up to request clarification or add additional context in comments.

3 Comments

Nice, it works. The only thing is that I can't set the properties to my embed view. I tried MySubClass *aView = [viewsFromNib objectAtIndex:0]; aView.myProp = 100 but is does not work. I think it actually changes the property value but its not "refreshing" view.
Setting properties on the view will not change the view unless you define behaviour in your view to update itself when the properties change, or tell it to redraw/re-layout itself with [view setNeedsLayout]' or setNeedsDisplay`. Check out my edit to read more on nibs so you can manage your expectations on how things should behave.
Nice, I was missing that custom setter on my label properties. Thanks!
1

André, this can be done with relative ease. make sure to import the class that you want to embed. then to create them with your normal

ClassName *subview=[[ClassName alloc]init]; 
[subview.view setFrame:CGRectMake(x,y,width,height)]; 
[self.view addSubview:subview.view]; 

which will add it to the x,y coordinates you specify with the size specified by your width, height. you can do this in the viewDidLoad or whenever you need them to be created.

1 Comment

I tried that already. This code gives me this error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Requesting the window of a view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.