开发者

Custom UIViewController and custom UIView

i have three views which have a couple of similarities. For example a UIImageView at position 30,30 and three UILabels at the same position.

Whats the best way to solve this without copy and paste or redundant code?

When i create all three views with a nib i have to copy and paste the same parts...

I started to implement my own UIViewController and a base UIView for that. My code looks like this:

- (id)init {

    if((self = [super init])) {

        image = [[UIImageView alloc] init];
        header = [[UILabel alloc] init];

        [self addSubview:image];
        [self addSubview:header];
    }

    return self;   
}

- (void)layoutSubviews {

    [super layoutSubviews];

    image.frame = CGRectMake(30, 30, 20, 20);

    header.frame = CGRectMake(30, 100, 200, 30);
}

When i push the controller with that view onto the stack, the view is displayed wrong. Image and label are covered by the titleheader (navigationcontroller)...

Can someon开发者_StackOverflowe please explain me how this layout system works... ios sdk is the worst sdk i ever worked with :(


I don't see the necessity for copy and paste while creating views in a nib? It definitely removes the necessity to position views per code. And as aside comment: ususally the "worst" sdks are those that we don't know well.

And if they need a common set of housekeeping code factot a method out that will take a view as a parameter and do it without offending the DRY principle (donz repeat yourself)


When i create all three views with a nib i have to copy and paste the same parts...

Do you realize that it's possible -- simple, even -- to have an object like a view controller load more than one nib? All three of your view controllers could load the common part of the interface from the same nib file, and also load their own interfaces from their own nib files. One way to do that would be to put the outlet(s) for the common interface in one class along with whatever code (setup, actions, etc.) you need to manage the common interface. You could then create a subclass of that class for each different view controller, and have each subclass load its own nib. The last step is to either insert the interface from the subclass into the superclass's view hierarchy or insert the interface from the superclass into the subclass's view hierarchy, whichever makes sense in your situation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜