夜晚本来像用代码写一个Button按钮,但是出现问题:开始是这样创建的
按照这个看似没问题,但是在模拟器上运行的时候,Button并没有出现,用模拟器分析显示Value stored to 'writeButton' during its initialization is never read(值存储到“writeButton”在其初始化是从来没有读过)
这样写在模拟器上也没有显示Button
当这样写的时候才出现Button在模拟器视图上,于是啄么着是Button初始化frame时候,没有确定Button类型(此处是UIButtonTypeRoundedRect),
难道是因为不需要初始化吗? 这个问题暂时不能理解
- (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,如需转载请自行联系原作者