重温for循环创建button

来源:互联网 发布:绵阳广电网络营业厅 编辑:程序博客网 时间:2024/06/08 05:00

代码段:

CGFloat x =20;

   CGFloat y = 50;

   NSArray *textArray =@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",];

   for (NSUInteger i =0;i < 9 ;i ++) {

       UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

        [buttonsetTitle:textArray[i] forState:UIControlStateNormal];

        button.frame =CGRectMake(x, y, 50,50);

        [self.viewaddSubview:button];

        [buttonaddTarget:selfaction:@selector(btn)forControlEvents:UIControlEventTouchUpInside];

        button.tag = i+1 ;

        x +=80;

       if (i == 2) {

            y +=60;

            x =20;

        }

       if (i == 5) {

            y +=60;

            x =20;

        }

    }



0 0
原创粉丝点击