代码写Button遇到问题

简介:
夜晚本来像用代码写一个Button按钮,但是出现问题:开始是这样创建的

- (void)viewDidLoad {         [super viewDidLoad];
   //初始化Button的位置大小     UIButton *writeButton = [[UIButton alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 50.0f, 30.0f)];     //设置BUtton的形状,此处为圆角按钮     writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];     //设置Button名字,和他的状态,此处是一般的状态     [writeButton setTitle:@"代码按钮" forState:UIControlStateNormal];     //Button背景色     writeButton.backgroundColor = [UIColor redColor];     //Button的触摸事件     [writeButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];     //把Button添加到视图之上     [self.view addSubview:writeButton]; } 

按照这个看似没问题,但是在模拟器上运行的时候,Button并没有出现,用模拟器分析显示Value stored to 'writeButton' during its initialization is never read(值存储到“writeButton”在其初始化是从来没有读过)


- (void)viewDidLoad {         [super viewDidLoad];     UIButton *writeButton = [[UIButton alloc] init];          CGRect frame = CGRectMake(100.0f, 100.0f, 50.0f, 30.0f);          writeButton.frame= frame;          writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];          [writeButton setTitle:@"代码按钮" forState:UIControlStateNormal];          writeButton.backgroundColor = [UIColor redColor];          [writeButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];          [self.view addSubview:writeButton]; } 



这样写在模拟器上也没有显示Button


- (void)viewDidLoad {         [super viewDidLoad];     UIButton *writeButton = [[UIButton alloc] init];          CGRect frame = CGRectMake(100.0f, 100.0f, 100.0f, 30.0f);                   writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];          writeButton.frame= frame;           [writeButton setTitle:@"代码按钮" forState:UIControlStateNormal];          writeButton.backgroundColor = [UIColor redColor];          [writeButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];          [self.view addSubview:writeButton]; } 




当这样写的时候才出现Button在模拟器视图上,于是啄么着是Button初始化frame时候,没有确定Button类型(此处是UIButtonTypeRoundedRect),

但是由product----》Analyze 又出现Value stored to 'writeButton' during its initialization is never read


再次修改

- (void)viewDidLoad {         [super viewDidLoad];               CGRect frame = CGRectMake(100.0f, 100.0f, 100.0f, 30.0f);     UIButton *writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];          //    writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];          writeButton.frame= frame;           [writeButton setTitle:@"代码按钮" forState:UIControlStateNormal];          writeButton.backgroundColor = [UIColor redColor];          [writeButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];          [self.view addSubview:writeButton]; }

难道是因为不需要初始化吗?  这个问题暂时不能理解



  本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208767,如需转载请自行联系原作者







相关文章
Element el-button 按钮组件详解
本文目录 1. 背景 2. 按钮分类 3. 按钮样式 4. 按钮状态 5. 按钮分组 6. 按钮尺寸 7. 小结
2885 0
Element el-button 按钮组件详解
|
6月前
input type="button"和button的区别
input type="button"和button的区别
|
小程序
button按钮组件
button按钮组件
html+css实战37-按钮button
html+css实战37-按钮button
102 0
html+css实战37-按钮button
|
JavaScript
关于button的小问题
本文主要讲关于button的小问题
|
JavaScript 前端开发
input type="button" 和button区别
来源: https://www.jianshu.com/p/687aabeedae5 在一个页面上画一个按钮,有四种办法: 1、 这就是一个按钮。
1458 0
|
内存技术 数据格式 XML