[英]Button title text doesn't clear
I have BtnDate
Button , when clicked, action works and gets current date with popup and adding inside BtnDate.titleLabel.text
and when I use inside my btnAddForm
action to check if _myText
is bigger than > 0
clear _BtnDate.titleLabel.text
我有
BtnDate
Button,当单击时,动作有效并通过弹出窗口获取当前日期并在BtnDate.titleLabel.text
添加,并且当我在btnAddForm
动作中使用以检查_myText
是否大于> 0
清除_BtnDate.titleLabel.text
But it doesn't clear the titleLabel
text string. 但是它不会清除
titleLabel
文本字符串。
My code is below. 我的代码如下。
I want, if the user entry _myText
inside > 0 value for _BtnDate
to be empty. 我想要的是,如果
_myText
的用户条目_myText
内> 0值, _BtnDate
空。
My entire code is below: 我的整个代码如下:
.h file inside: .h文件里面:
@property (nonatomic,retain) IBOutlet UIButton* BtnDate;
.m file inside: .m文件里面:
- (void)viewDidLoad
{
[super viewDidLoad];
[_BtnDate addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
_BtnDate.titleLabel.font = [UIFont systemFontOfSize:17];
[[_BtnDate layer] setBorderWidth:1];
[[_BtnDate layer] setCornerRadius:5];
[[_BtnDate layer] setBorderColor:[UIColor lightGrayColor].CGColor];
[_BtnDate setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:_BtnDate];
}
-(void) showDateView{
viewBack2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[viewBack2 setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
[self.view addSubview:viewBack2];
dateView = [[UIView alloc] initWithFrame:CGRectMake((viewBack2.frame.size.width-300)/2, (viewBack2.frame.size.height-350)/2, 300, 350)];
[self.view addSubview:dateView];
UIImageView *imageBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-popup"]];
[imageBackground setFrame:CGRectMake(0, 0, 300, 350)];
[dateView addSubview:imageBackground];
[imageBackground release];
UIButton *BtnDate = [UIButton buttonWithType:UIButtonTypeCustom];
[BtnDate setBackgroundImage:[UIImage imageNamed:@"okay.png"] forState:UIControlStateNormal];
[BtnDate addTarget:self action:@selector(btnDateAction:) forControlEvents:UIControlEventTouchUpInside];
[BtnDate setFrame:CGRectMake(80, 300, 140, 36)];
[dateView addSubview:BtnDate];
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(25,25, 250, 250)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
[datePicker addTarget:self
action:@selector(changeDateInLabel:)
forControlEvents:UIControlEventValueChanged];
[dateView addSubview:datePicker];
}
- (void)changeDateInLabel:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
if (_BtnDate.selected==true) {
[_BtnDate setTitle:[NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]] forState:UIControlStateNormal];
}
[df release];
}
-(IBAction)btnDateAction:(id)sender{
if (_BtnDate.selected==true) {
_BtnDate.selected=false;
}
[dateView removeFromSuperview];
[viewBack2 removeFromSuperview];
}
- (void) buttonPressed:(UIButton*)sender {
if (sender == _BtnDate) {
[self showDateView];
_BtnDate.selected=true;
}
}
- (IBAction)btnAddForm:(id)sender {
// I WANT TO CHECK _myText if is not null or > 0 must be clear _BtnDate.titleLabel.text dont do this !
if ([NSNumber numberWithInt:[_myText.text intValue]] > 0 ) {
_BtnDate.titleLabel.text = @"";
}
}
- (void)dealloc {
[_myText release];
[_BtnDate release];
[super dealloc];
}
- (void)viewDidUnload {
[self setmyText:nil];
[self setBtnDate:nil];
[super viewDidUnload];
}
when you click on btnAddForm
当您单击
btnAddForm
set this 设置这个
[BtnDate setTitle:@"" forState:UIControlStateNormal];
You always need to specify ControlState when updating button's title! 更新按钮标题时,您始终需要指定ControlState! There are four possible values for UIButtons:
UIButton有四个可能的值:
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
So Try this 所以试试这个
[BtnDate setTitle:@"" forState:UIControlStateNormal];
In showDateView you use a local variable BtnDate to create your button and install it in your dateView. 在showDateView中,您使用局部变量BtnDate创建按钮并将其安装在dateView中。 As a result the
BtnDate
property is probably nil. 结果,
BtnDate
属性可能为零。
Change your code that creates the button to use the instance variable. 更改创建按钮的代码以使用实例变量。
_BtnDate = [UIButton buttonWithType:UIButtonTypeCustom];
[_BtnDate setBackgroundImage:[UIImage imageNamed:@"okay.png"] forState:UIControlStateNormal];
[_BtnDate addTarget:self action:@selector(btnDateAction:) forControlEvents:UIControlEventTouchUpInside];
[_BtnDate setFrame:CGRectMake(80, 300, 140, 36)];
[dateView addSubview:_BtnDate];
By the way, properties and other variable names should start with a lower-case letter, so BtnDate
should be btnDate
. 顺便说一句,属性和其他变量名称应以小写字母开头,因此
BtnDate
应该为btnDate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.