Iphone 状态不工作的UIButton设置标题

Iphone 状态不工作的UIButton设置标题,iphone,xcode4,uibutton,settext,Iphone,Xcode4,Uibutton,Settext,我试图在新视图上设置UIButton的文本,当从上一个视图中按下“Next”按钮时。我已经正确设置了IBOutlet,我已经到处寻找答案,但它根本不起作用 以下是我尝试做的一个示例: - (IBAction)computeQuiz:(id)sender { [fiveFootUniversalResults setTitle:@"Test" forState:UIControlStateNormal]; } - (IBAction)jumpT10ResultsView:(id)se

我试图在新视图上设置UIButton的文本,当从上一个视图中按下“Next”按钮时。我已经正确设置了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设置正确,我只是不知道我做错了什么。有什么想法吗?

试试这段代码

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];
试着打电话

- (IBAction)computeQuiz:(id)sender 
中的方法

- (IBAction)jumpT10ResultsView:(id)sender
方法,而不是同时调用button click,即使与下面的代码一样,调用方法me也不会导致分配fiveFootUniversalResults button对象

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

如果您忘记使用按钮上的
@synthesis
或与其相关的变量,也可能发生这种情况。

它不起作用,因为IB设置的是
属性标题
,而不是
标题

请尝试以下方法:

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];
(第二个选项不会保留格式。)

尝试:

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

可能背景色与标题颜色相同。当你用黑笔在黑纸上写字时,你什么也看不见。在调用setTitle之前,请尝试设置背景颜色或标题颜色。

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


您确定您的操作已设置好吗?插入断点并进行验证。我100%确定已正确设置IBOutlets,我希望iActions也已正确连接?下一个视图显示,我已将NSLogs放置在显示的ComputeQuike操作中。正在调用操作。在这种情况下,
fiveFootUniversalResults
有问题。这是roundedRect按钮吗?它有图像吗?@Link你有没有让它工作过?我遇到了完全相同的问题。仍然不确定为什么4年后这不是正确的答案!我觉得很傻,我必须找到这个来解决它,但可能节省了我几个小时的调试。