创建按钮的两种方法

1、动态创建

1 btnfont = [UIButton buttonWithType:UIButtonTypeRoundedRect];
2     [btnfont setFrame:CGRectMake(1001012040)];
3     [btnfont addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
4     [btnfont setTitle:@"字体" forState:UIControlStateNormal];
5     btnfont.backgroundColor=[UIColor clearColor];
6     [self.view addSubview:btnfont];
7 

8   

2、在xib文件中已经创建好,通过tag获取按钮 

UIButton *testButton= (UIButton*)[self.view viewWithTag:100];
    [testButton addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];

  

注册事件

-(void) test: (id) sender{
    UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"ceshi" message:@"test11111" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease];
    [av show];
}

  

posted @ 2011-10-06 10:37  威风剑  阅读(543)  评论(0编辑  收藏  举报