按钮创建
//button创建 -(void)initUI { NSArray *imageArr =[NSArray arrayWithObjects:@"hf",@"zfb",@"cft",@"yhk", nil]; //正常状态 NSArray *hImageArr =[NSArray arrayWithObjects:@"hf0",@"zfb0",@"cft0",@"yhk0", nil]; //高亮 NSArray *labelArr =[NSArray arrayWithObjects:@"兑换话费",@"支付宝",@"财付通",@"银行卡", nil]; //选项 for (int i=0; i<4; i++) { UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:[imageArr objectAtIndex:i]] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageNamed:[hImageArr objectAtIndex:i]] forState:UIControlStateHighlighted]; //视图布局 button.frame = CGRectMake(45+150*i, 100, 142/2,142/2); if (i>1) { button.frame = CGRectMake(45+150*(i-2),260, 142/2,142/2); } button.tag=i; [button addTarget:self action:@selector(buttonClick:) forControlEvents: UIControlEventTouchUpInside]; [self.view addSubview:button]; UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(button.frame.origin.x-10,button.frame.origin.y+80 , 100, 30)]; label.text=[labelArr objectAtIndex:i]; label.textAlignment=NSTextAlignmentCenter; label.backgroundColor=[UIColor clearColor]; label.textColor=[UIColor blackColor]; [self.view addSubview:label]; } }
//点击事件 -(void)buttonClick:(UIButton *)btn { switch (btn.tag) { case 0: NSLog(@"兑换话费"); break; case 1: NSLog(@"支付宝"); break; case 2: NSLog(@"财付通"); break; case 3: NSLog(@"银行卡"); break; default: break; } }
对Button进行一次封装 http://blog.csdn.net/duxinfeng2010/article/details/8687203