IOS 控件 UIButton button

学了几天了,今天复习整理一下,虽然掌握的不全面,但还是要总结一下,好慢慢积累补充

UIButton是UIControl的一种

创建方法有两种  

一种是动态的创建

UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80 , 50)];

//设置button1标题和背景图片

[button1 setTitle:@"button1" forState:UIControlStateNormal];

[button1 setBackgroundImage:[UIImage imageNamed:@"pic"] forState:UIControlStateHighlighted];

//按下按钮的特效   

button1.adjustsImageWhenHighlighted = NO;    

button1.adjustsImageWhenDisabled = NO;   

button1.showsTouchWhenHighlighted = YES;

另一种是静态的创建

  UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//初始化设置

//设置button2的位置和大小 

button2.frame = CGRectMake(110, 0, 50, 50);

[button2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];

//forState属性决定一些显示特性在何种条件下显示

添加触摸事件    

[button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];       

  [button2 addTarget:self action:@selector(buttonPressed2:) forControlEvents:UIControlEventTouchUpInside];

完整代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80 , 50)];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    button2.frame = CGRectMake(110, 0, 50, 50);
    
    [button1 setTitle:@"button1" forState:UIControlStateNormal];
    [button2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

    [button1 setBackgroundImage:[UIImage imageNamed:@"pic"] forState:UIControlStateHighlighted];
    button1.adjustsImageWhenHighlighted = NO;
    button1.adjustsImageWhenDisabled = NO;
    button1.showsTouchWhenHighlighted = YES;
    
    [self.view addSubview:button1];
    [self.view addSubview:button2];
    [button1 release];
    
    [button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    [button2 addTarget:self action:@selector(buttonPressed2:) forControlEvents:UIControlEventTouchUpInside];
}


- (IBAction)buttonPressed:(id)sender
{
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"my view" message:@"点击BUTTON1" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert1 addButtonWithTitle:@"取消"];
    [alert1 show];
}
- (IBAction)buttonPressed2:(id)sender
{
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"my view" message:@"点击BUTTON2" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert1 addButtonWithTitle:@"取消"];
    [alert1 show];
}


 暂时学习这么多,有新的发现了再继续补充

[button1 release]是因为创建的时候alloc,button1对象的retainCount为1  addSubview一次 会把传过去的对象retain一次,那么retainCount为2,释放一次 button1的所有者只剩下self.view retainCount为1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值