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?

有帮助吗?

解决方案

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

其他提示

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

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

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

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?

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top