0

I want that when user enter wrong value in the textfiled it shows alert and it makes textfield emty and also the button title to empty.In my code textfield empty is done but button is not getting the empty values

below is the my code

     if (monthText>totalnumber) {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Value Must Be Less than total number of dogs " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    NSString *titleMonth =@"0";
    [cereniaButton setTitle:@"" forState:UIControlStateNormal];






    cereniaTextField.text=@"";


  }


int year=12;
int monthButtonTitle=monthText*year;
NSString *titleMonth =[NSString stringWithFormat:@"%d",monthButtonTitle];
[cereniaButton setTitle:titleMonth forState:UIControlStateNormal];
james
  • 646
  • 3
  • 9
  • 24
  • you could try to assign a " " with NSString *titleMonth =@" "; – CarlJ Jun 06 '12 at 10:15
  • can you post a NSLog(@"%@", cereniaButton); – CarlJ Jun 06 '12 at 10:18
  • try this [cereniaButton setTitle:[NSString stringWithFormat:@"%@", titleMonth] forState:UIControlStateNormal]; – Nikunj Jadav Jun 06 '12 at 10:18
  • not working this i may tell you that i am using this code on textdidchange of the textfield when i enter the textbox value the title changes but when it shows alert i want empty it does not do like this – james Jun 06 '12 at 10:23
  • print the value of titleMonth.If the data is there definitely it should display – Tendulkar Jun 06 '12 at 10:25
  • Check if you have connected the outlet or not – superGokuN Jun 06 '12 at 10:27
  • i have connected outlet then why it is changing title when the value is correct and don't show alert – james Jun 06 '12 at 10:28
  • @Tendulkar it is showing data but not showing on the button title – james Jun 06 '12 at 10:31
  • Does this answer your question? [Xcode 13 - "Button" title not disappearing](https://stackoverflow.com/questions/69375584/xcode-13-button-title-not-disappearing) – Fattie May 27 '22 at 12:30
  • ## It's a well-known bug https://stackoverflow.com/a/69375669/294884 for the workaround – Fattie May 27 '22 at 12:30

4 Answers4

0

What happens if you directly assign like

[btn setTitle:@"" forState:UIControlStateNormal];

Dhruv
  • 2,153
  • 3
  • 21
  • 45
  • then may be it is possible that old value must be stored in some string and when the above line is executed,there might be some code that would place that string back as title of button. – Dhruv Jun 06 '12 at 10:35
  • You are adding new value at last.check it out,so the button gets back the value at bottom of code. – Dhruv Jun 06 '12 at 10:36
  • i also like this that when shows alert get@"" this value other wise the last value – james Jun 06 '12 at 10:52
  • 1
    but according to your code,after coming out of condition,the other code is also fulfilled,write other part in else .like : if (monthText>totalnumber) { ..... cereniaTextField.text=@""; } else{ int year=12; int monthButtonTitle=monthText*year; NSString *titleMonth =[NSString stringWithFormat:@"%d",monthButtonTitle]; [cereniaButton setTitle:titleMonth forState:UIControlStateNormal];} – Dhruv Jun 06 '12 at 11:01
0

Write this line after setting the title to button

[self.view setNeedsDisplay];
Emil
  • 7,220
  • 17
  • 76
  • 135
Tendulkar
  • 5,550
  • 2
  • 27
  • 53
0

you are resetting the title but after your title will ripopolate.

try

if (monthText>totalnumber) {
    //...yourcode...

   return;
}

let me know

Simone Pistecchia
  • 2,746
  • 3
  • 18
  • 30
0

might be u are missing to connect button with file owner , try this :

  1. Declare IBOutlet to button in .h file , button that is created by xib.
  2. @synthesize btn in .m
  3. connect button (btn) to file Owner in xib . and use this delegate:

-(void)textFieldDidEndEditing:(UITextField *)textField

 int txtValue = [textField.text intValue];
 if(txtValue < 10)


     btn.titleLabel.text = [NSString stringWithFormat:@"%d",txtValue];
        else 
        {
            btn.titleLabel.text = [NSString stringWithFormat:@""];
        } 
    }
    - (BOOL)textFieldShouldReturn: (UITextField *)textField 
    {
        [textField resignFirstResponder];
        return YES;
    }
Maulik
  • 19,348
  • 14
  • 82
  • 137
kulss
  • 2,057
  • 1
  • 14
  • 16