1

I have these two properties defined in my appDelegate:

@property (nonatomic, strong) UIView * mainView;
@property (nonatomic, strong) MyCustomClass * customObj

What is the best way of adding subviews to mainView from code inside customObj?

I am not providing any sample code because (a) my code is terrible and (b) I just want to understand the best approach of doing this, so I can learn from this in the future, rather than having one solution to one specific problem.

Many thanks.

1
  • Explain it better + give some solution you've tried Commented Nov 27, 2013 at 9:30

2 Answers 2

2

well, what about creating a method in MyCustomClass like this:

-(void)addSubViewToView:(UIView *)view
{
    [view addSubview:otherView];
}

And then call it like this:

[customObj addSubViewToView:mainView];
Sign up to request clarification or add additional context in comments.

1 Comment

Or init customObj with that view - if main class creates that object.
1

It depends on what kind of class MyCustomClass is. Is it responsible for building mainView's view hierarchy? Then I'd inject a reference of mainView to customObj, like this:

customObj = [[MyCustomClass alloc] initWithView:mainView];

In this scenario, customObj would be some kind of builder object, that creates the view hierarchy inside mainView. Then I'd use the addSubView: selector inside MyCustomClass:

-(id)initWithView:(UIView*)view{
    if(self = [super init]){
        [view addSubView: ...];
        [view addSubView: ...];
        [view addSubView: ...];
    }
}

Comments

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.