UIButton setTitle forState Not Working

时间:2011-11-02 19:17:53

标签: iphone xcode4 uibutton settext

当我从前一个视图中按下“下一步”按钮时,我正在尝试在新视图上设置UIButton的文本。我已经正确地设置了IBOutlet,我已经全神贯注地寻找答案,但它根本就没有用。

以下是我正在尝试做的一个示例:

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

这些操作都连接到上一个视图上的按钮。视图加载精细和一切,唯一的问题是文本不会在按钮上更改。我100%肯定我的IBOutlets设置正确我只是不知道我做错了什么。有什么想法吗?

6 个答案:

答案 0 :(得分:24)

由于IB设置了attributedTitle而不是title,因此无效。

请改为尝试:

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

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

或者,或者:

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

(第二个选项不会保留您的格式。)

答案 1 :(得分:3)

尝试此代码....

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

答案 2 :(得分:1)

尝试拨打

- (IBAction)computeQuiz:(id)sender 

中的

方法

- (IBAction)jumpT10ResultsView:(id)sender

方法而不是同时在按钮上单击调用,即使在下面的代码中同时调用,方法调用也导致没有分配fiveFootUniversalResults按钮对象。

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

答案 3 :(得分:0)

如果您忘记使用按钮上的@synthesize或与之相关的变量,也会发生这种情况。

答案 4 :(得分:0)

当我遇到同样的问题时,以下链接中的答案帮助了我。它允许我保存字典中的所有属性。我创建了一个具有不同文本但属性相同的新属性字符串,然后我将其添加到我想要更改的按钮中。希望这会有所帮助。

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

答案 5 :(得分:-1)

尝试:

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

背景颜色可能与标题颜色相同。当你用黑色钢笔在黑纸上书写时,你不会看到任何东西。尝试在调用setTitle之前设置背景颜色或标题颜色。