有时候无法从控件中拖拽一个按钮到storyboard,必须用编写代码方式添加按钮:
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view, typically from a nib. 5 //计算出展示表情区域的宽和 展示区距顶部的高度+10个偏移量 6 //添加按钮 9 //创建button 10 addBtn = [[UIButton alloc] init]; 11 addBtn.frame = CGRectMake(0, 0, 60, 60); 12 [self.view addSubview:addBtn]; //添加按钮对象到view列表中去 15 //2.设置背景图片 16 UIImage *addImageNormal = [UIImage imageNamed:@"019.png"]; 17 [addBtn setBackgroundImage:addImageNormal forState:UIControlStateNormal]; 18 //3.设置点击后的图片 19 UIImage *addImageHighed = [UIImage imageNamed:@"020.png"]; 20 [addBtn setBackgroundImage:addImageHighed forState:UIControlStateHighlighted]; 21 //4.添加tag 22 addBtn.tag = 1; 23 //5.添加监听方法 24 [addBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside]; 25 } 26 27 //实现监听的方法 28 - (void)addClick{ 29 NSLog(@"测试:我被点击了!!!");50 }
原文:http://www.cnblogs.com/litaowei/p/3695418.html