简体   繁体   English

自定义UIViewController和自定义UIView

[英]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. 例如,位置30,30处的UIImageView和相同位置处的三个UILabel。

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. 我开始实现自己的UIViewController和一个基本的UIView。 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)... 图片和标签由titleheader(navigationcontroller)覆盖...

Can someone please explain me how this layout system works... ios sdk is the worst sdk i ever worked with :( 有人可以解释一下这个布局系统的工作原理吗?ios sdk是我曾经使用过的最差的sdk :(

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) 如果他们需要一套通用的内务处理代码要素,则可以采用一种方法,该方法将视图作为参数并在不违反DRY原理的情况下进行操作(顿兹重复您自己)

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. 您的所有三个视图控制器都可以从同一个nib文件加载接口的公共部分,也可以从自己的nib文件加载它们自己的接口。 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. 最后一步是将子类中的接口插入超类的视图层次结构中,或将父类中的接口插入子类的视图层次结构中,以您所处的情况为准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM