个人理解:笔记

来源:互联网 发布:javascript 遍历json 编辑:程序博客网 时间:2024/06/11 02:57

笔记1:

自定义空间一般继承与UIview,需要实现的方法有

-(id)initWithFrame:(CGRect)frame

{

  slef = [super initWithFrame:frame];

  if(self)

  {

      //输入加载这个控件时你想要现实的内容,类似于ViewController的-(void)ViewDidLoad方法。

      //一般实例化的对象的Frame先设置成CGRectzero(空)。

      UILabel *label = [[UILabel alloc]initWithFrame:CGRectzero];

      label.backgroundColor = [UIColor clearColor];

      [self addSubview:label];

      self.label = label;

      //然后在-(void)layoutSubviews方法中设置他们的Frame,这样在其他View中调用他们的时候他们才会被显示出来。

      //如果是很多相同的控件,比如说7个UILabel,就用for循环,将它们先实例化Frame设置为CGRectzero,然后放在一个可变的数组(NSMutableArray)中,然后在-(void)layoutSubview是用数组的遍历,来一一设置他们的Frame属性。

      NSMutableArray *labels = [NSMutable array];

            for(int i = 0;i < 7; i++)

      {

         UILabel *otherLabel = [[UILabel alloc]initWithFrame:CGRectzero];

         otherLabel.backgroundColor = [UIColor clearColor];

         [self addSubviews:otherLabel];

         [labels addObject:otherLabel];

      }

    self.labels = labels;

  }

   return self;

   //返回我们定义的这个控件

}

-(void)layoutSubviews

{

      self.label.frame = CGRectMake(0,200,200,200);

      //设置label的Frame。

      int i = 0;

      for(UILabel *otherLabel in self.labels)

      {

           otherLabel.frame = CGRectMake(i*20,0,20,20);

           i++;

      }

      是用遍历从第一个otherLabel开始一次设置他们的Frame。

}

系统会自动调用-(void)layoutSubviews方法。

0 0
原创粉丝点击