动态添加按钮

来源:互联网 发布:疯狂美工破解版 编辑:程序博客网 时间:2024/05/21 09:45

1.动态添加按钮

for (int i =0; i<self.arrayPin.count; i++) {

       NSString *xnum = [[self.arrayPinobjectAtIndex:i] objectForKey:@"x"];

       NSString *ynum = [[self.arrayPinobjectAtIndex:i] objectForKey:@"y"];

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom]; 

       UIImage *buttonimage = [UIImageimageNamed:@"needle1"];

        [buttonsetFrame:CGRectMake([xnumintValue]-buttonimage.size.width, [ynumintValue]- buttonimage.size.height, buttonimage.size.width, buttonimage.size.height)];   

        [button setImage:buttonimageforState:UIControlStateNormal];

        [buttonsetTag: i+1];

        [button addTarget:selfaction:@selector(action:)forControlEvents:UIControlEventTouchUpInside];


[buttonArrayaddObject:button];//按钮加到数组里

    }

2.遍历动态添加的按钮

[buttonArrayenumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){

       UIButton *button =obj;

        [buttonsetHidden:YES];

    }];


0 0
原创粉丝点击