黑马程序员技术交流社区

标题: 自己创建按钮的代码 [打印本页]

作者: 一步步往上爬    时间: 2016-1-17 12:42
标题: 自己创建按钮的代码
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建一个按钮
    UIButton * button = [[UIButton alloc] init];
    //设置按钮的位置和大小
    button.frame = CGRectMake(100, 100, 250, 250);
    //设置默认按钮文字
    [button setTitle:@"点我试试" forState:UIControlStateNormal];
    //设置默认按钮文字颜色
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    //设置默认背景
    [button setBackgroundImage:[UIImage imageNamed:@"luffy"] forState:UIControlStateNormal];
    //设置高亮状态下的文字
    [button setTitle:@"再点我砍了你" forState:UIControlStateHighlighted];
    //设置高亮状态下的文字的颜色
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    //设置高亮状态下的背景
    [button setBackgroundImage:[UIImage imageNamed:@"zoro"] forState:UIControlStateHighlighted];
    //把创建的button对象添加到self.view
    [self.view addSubview:button];
    // 4.监听按钮点击(点击按钮后就会调⽤用self的buttonClicked⽅方法)
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
   
   }
-(void)buttonClicked:(UIButton *)button{
    NSLog(@"按钮被点击了,文字是:%@",button.currentTitle);
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2