IOS-创建Button简单实例

        CGRect frame;
        MyButton *btn = [[MyButton buttonWithType:UIButtonTypeCustom] retain];
        btn.index = i;
        btn.value = [NSString stringWithFormat:@"name%d",i];
        NSString *named = [array objectAtIndex:i];
        NSURL *url2 = [NSURL URLWithString:named];
        UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url2]];
        [btn setImage:img forState:UIControlStateNormal];
      
                frame.size.width = 120;
                frame.size.height = 200;
                frame.origin.x = (i%4)*120+20;
                frame.origin.y = i/4*120+20;
                break;


        [btn setFrame:frame];
        
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        [btn release];

posted on 2012-06-05 12:26  tx天翔  阅读(2106)  评论(0编辑  收藏  举报