2016-11-28 68 views
1

我有BtnDate按鈕,點擊後行爲作品,並得到當前的日期與彈出窗口並增加內部BtnDate.titleLabel.text,當我用我的行動btnAddForm裏面,以檢查是否_myText是大於> 0明確_BtnDate.titleLabel.text按鈕標題文字不清晰

但它不清除titleLabel文本字符串。

我的代碼如下。

我想要的,如果用戶條目_myText裏面> 0的值爲_BtnDate爲空。

我的整個代碼如下:

的.h文件中:

@property (nonatomic,retain) IBOutlet UIButton* BtnDate; 

的.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]; 
    } 
+0

@Rajat在那裏我會增加嗎? TY – SwiftDeveloper

回答

2

當你點擊btnAddForm

這集

[BtnDate setTitle:@"" forState:UIControlStateNormal]; 
1

你總是需要更新按鈕的標題時指定了ControlState-!有四種可能的值UIButtons:

UIControlStateNormal 
UIControlStateHighlighted 
UIControlStateDisabled 
UIControlStateSelected 

那麼試試這個

[BtnDate setTitle:@"" forState:UIControlStateNormal]; 
1

在showDateView您使用本地變量BtnDate創建您的按鈕,並在您的dateView安裝。因此BtnDate財產可能是零。

更改創建按鈕以使用實例變量的代碼。

_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]; 

順便說一句,性質和其他變量的名字應該用小寫字母開頭,所以BtnDate應該btnDate