在UIButton定义中隐藏实例变量'警告

时间:2014-02-16 20:32:29

标签: ios uibutton cgrect

我正在以编程方式将UIButton添加到datePicker:

 //today button
 CGRect todayButtonFrame = CGRectMake(180.0, 200.0, 40.0, 40.0);
 UIButton *todayButtonDatePicker = [[UIButton alloc]initWithFrame:todayButtonFrame];
 [todayButtonDatePicker addTarget:self action:@selector(myCustomFunction:) forControlEvents:UIControlEventTouchUpInside];
 [todayButtonDatePicker setBackgroundImage:[UIImage imageNamed:@"today.png"] forState:UIControlStateNormal];

[self.view addSubview: todayButtonDatePicker];

然而,编译器正在给我一个警告:

Local declaration of 'todayButtonDatePicker' hides instance variable

1 个答案:

答案 0 :(得分:2)

如果您想将UIButton保留为属性,请执行以下操作:

 //today button
 CGRect todayButtonFrame = CGRectMake(180.0, 200.0, 40.0, 40.0);
 self.todayButtonDatePicker = [[UIButton alloc]initWithFrame:todayButtonFrame];
 [self.todayButtonDatePicker addTarget:self action:@selector(myCustomFunction:)forControlEvents:UIControlEventTouchUpInside];
 [self.todayButtonDatePicker setBackgroundImage:[UIImage imageNamed:@"today.png"] forState:UIControlStateNormal];

 [self.view addSubview: self.todayButtonDatePicker];

或者,您可以删除该属性,只使用局部变量。