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?

Alexey Kozhevnikov
  • 4,249
  • 1
  • 21
  • 29
Link
  • 396
  • 1
  • 4
  • 15
  • *I'm 100% sure I have the IBOutlets set correctly*, I hope IBActions are connected correctly too? – beryllium Nov 02 '11 at 19:22
  • Are you sure that your Action is set? Put a break point in and verify. – logancautrell Nov 02 '11 at 19:20
  • The next view shows up and I have placed NSLogs in my computeQuiz action that show up. The actions are getting called. – Link Nov 02 '11 at 19:29
  • In this case something is wrong with `fiveFootUniversalResults`. is this roundedRect button, does it has image? – beryllium Nov 02 '11 at 20:40
  • 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. – Link Nov 02 '11 at 22:36
  • Do you invoke the computeQuiz: method before or after the view appears? – Alexander Nov 11 '11 at 14:43
  • Can you debug this with this code if(fiveFootUnivarsalResults) NSLog(@"button exists"); else NSLog(@"btn does not exists"); – Bharat Jagtap Dec 16 '11 at 11:11
  • How many buttons are you using & which button title you are trying to change? – Anand Dec 16 '11 at 11:14
  • 1
    @Link Did you ever get this working? I'm running into the exact same problem. – Dan P. Dec 09 '12 at 10:47
  • 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... – Big Papoo Mar 17 '14 at 15:31

6 Answers6

35

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.)

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
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];
Anshul Jain
  • 903
  • 6
  • 9
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];
}
0

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

Daniel Lima
  • 798
  • 8
  • 13
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?

Community
  • 1
  • 1
antonkronaj
  • 1,237
  • 12
  • 13
-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.

shim
  • 9,289
  • 12
  • 69
  • 108
dadoftutu
  • 49
  • 4