UIButton setTitle forState Not Working
Asked Answered
U

6

18

I'm trying to set the text of a UIButton on a new view when a "Next" button is pressed from a previous view. I have set the IBOutlet correctly and I've looked all over for answers to this but it just isn't working at all.

Here is a sample of what I'm trying to do:

- (IBAction)computeQuiz:(id)sender
{  
    [fiveFootUniversalResults setTitle:@"Test" forState:UIControlStateNormal];
}

- (IBAction)jumpT10ResultsView:(id)sender
{       
    NSString *nibFileToLoad = @"JumpT10Results";

    UIDevice *device = [UIDevice currentDevice];

    if([device userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        nibFileToLoad = [nibFileToLoad stringByAppendingString:@"-iPad"];
    }

    JumpmasterPathfinderViewController *nextView = [[JumpmasterPathfinderViewController    alloc] initWithNibName:nibFileToLoad bundle:nil];

    // Modal view controller
    nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:nextView animated:YES];

    [nextView release];

    [scrollView setContentSize:CGSizeMake(imageViewWidth, imageViewHeight + 44.0)];
}

These actions are both connected to a button on a previous view. The view loads fine and everything, the only problem is the text WILL NOT change on the button. I'm 100% sure I have the IBOutlets set correctly I just don't know what I am doing wrong. Any ideas?

Uranometry answered 2/11, 2011 at 19:17 Comment(10)
Are you sure that your Action is set? Put a break point in and verify.Toothwort
I'm 100% sure I have the IBOutlets set correctly, I hope IBActions are connected correctly too?Bonnee
The next view shows up and I have placed NSLogs in my computeQuiz action that show up. The actions are getting called.Uranometry
In this case something is wrong with fiveFootUniversalResults. is this roundedRect button, does it has image?Bonnee
At first I had it as a custom button. Then, since nothing was working, I deleted the button and created a new default roundedRect button and attached the IBOutlet to it. Still nothing.Uranometry
Do you invoke the computeQuiz: method before or after the view appears?Vetchling
Can you debug this with this code if(fiveFootUnivarsalResults) NSLog(@"button exists"); else NSLog(@"btn does not exists");Apyretic
How many buttons are you using & which button title you are trying to change?Caesura
@Uranometry Did you ever get this working? I'm running into the exact same problem.Wimer
Could it be iOS 7.1 ? I'm having strange behavior too since iOS 7.1. Need to set both Normal and Selected values. Prior to 7.1, setting only Normal was enough as a default value for all states... Ooops, sorry, just saw it was posted in 2011...Originality
B
36

It's not working because IB sets attributedTitle instead of title.

Try this instead:

NSAttributedString *attributedTitle = [self.myButton attributedTitleForState:UIControlStateNormal];
NSMutableAttributedString *mas = [[NSMutableAttributedString alloc] initWithAttributedString:attributedTitle];
[mas.mutableString setString:@"New Text"];

[self.myButton setAttributedTitle:mas forState:UIControlStateNormal];

Or, alternatively:

[self.myButton setAttributedTitle:nil forState:UIControlStateNormal];
[self.myButton setTitle:@"New Text" forState:UIControlStateNormal];

(The second option won't preserve your formatting.)

Berriman answered 26/11, 2013 at 20:3 Comment(2)
Still not sure why this is not marked the correct answer after 4 Years!Ewell
Feeling dumb that I had to reach this to figure it out, but saved me probably hours of debugging.Jeuz
A
3

try this code....

UIButton *backbtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 68, 38)];
[backbtn setTitle:@"Your string" forState:UIControlStateNormal];
//[backbtn setImage:[UIImage imageNamed:@"BackBtn.png"] forState:UIControlStateNormal];
[backbtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backbtn];
Angloamerican answered 16/12, 2011 at 10:59 Comment(0)
M
1

Try calling

- (IBAction)computeQuiz:(id)sender 

method in the

- (IBAction)jumpT10ResultsView:(id)sender

method rather then calling simultaneously on button click even as simultaneous as in below code, call of method me result in no allocation of fiveFootUniversalResults button object.

- (IBAction)computeQuiz:(id)sender
{  
    [fiveFootUniversalResults setTitle:@"Test" forState:UIControlStateNormal];
}

- (IBAction)jumpT10ResultsView:(id)sender
{       
    NSString *nibFileToLoad = @"JumpT10Results";

    UIDevice *device = [UIDevice currentDevice];

    if([device userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        nibFileToLoad = [nibFileToLoad stringByAppendingString:@"-iPad"];
    }
    JumpmasterPathfinderViewController *nextView = [[JumpmasterPathfinderViewController    alloc] initWithNibName:nibFileToLoad bundle:nil];

    // Modal view controller
    nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:nextView animated:YES];

    [nextView release];

    [scrollView setContentSize:CGSizeMake(imageViewWidth, imageViewHeight + 44.0)];
    [self computeQuiz:nil];
}
Minute answered 6/12, 2012 at 12:49 Comment(0)
T
0

This can also happen if you forget to use the @synthesize on the button or variables related to it.

Tret answered 4/4, 2013 at 3:29 Comment(0)
C
0

The answer in the link below helped me when I had the same problem. It allowed me to save all of the attributes in a Dictionary. I created a new attributed string with different text but same attributes, which then I added to the button that I wanted changed. Hope this helps.

change the text of an attributed UILabel without losing the formatting?

Chlorate answered 25/2, 2015 at 13:49 Comment(0)
S
-1

try:

[fiveFootUniversalResults setBackgroundColor:[UIColor redColor]];
[fiveFootUniversalResults setTitle:@"Test" forState:UIControlStateNormal];

Maybe the background color is the same as the title color. You will not see anything when you writing on black paper with a black pen. Try to set the background color or the title color before calling setTitle.

Strickler answered 25/3, 2014 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.