tags:

views:

18

answers:

1

I've created a UIView and can add things to it by using [self.topView addSubview:image]; Now I am importing a class to create a calendar which has a heap of buttons. I could put it in the same class and say [self.topView addSubview:button] but if its in another class how do I add it to the subview of the class that owns it? Hope that makes sense...

+1  A: 

You need a reference in your external class to the class that owns the view (call it the "owner class"), and presumably write a method in your owner class to add a passed-in view to your chosen subview. Something along the lines of:

- (void) insertSubview:(UIView*)newView { 
   if (newView) [self.topView addSubview:newView];
}

Setting up the reference can be done a number of ways, so I'll leave that one to you.

rpj
Are you saying I need to pass in the subview to the external class?
Rudiger
You could pass the subview into the other class, but that would break encapsulation, which is bad practice. I was suggesting you add a method to the class that owns the subview, and use it to allow the owner class to perform the work necessary to get the view added to the hierarchy.
rpj