【问题标题】:UIButton setTitle forState Not WorkingUIButton setTitle forState 不工作
【发布时间】:2011-12-20 15:04:27
【问题描述】:

当从前一个视图按下“下一步”按钮时,我试图在新视图上设置 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 我只是不知道我做错了什么。有什么想法吗?

【问题讨论】:

  • 您确定您的操作已设置吗?插入断点并验证。
  • 我 100% 确定我的 IBOutlets 设置正确,我希望 IBActions 也正确连接?
  • 显示下一个视图,我已将 NSLogs 放置在显示的 computeQuiz 操作中。正在调用这些操作。
  • 在这种情况下,fiveFootUniversalResults 有问题。这是roundedRect按钮吗,有图片吗?
  • @Link 你有没有得到这个工作?我遇到了完全相同的问题。

标签: iphone xcode4 uibutton settext


【解决方案1】:

它不起作用,因为 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];

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

【讨论】:

  • 仍然不知道为什么这在 4 年后没有被标记为正确答案!
  • 感觉很愚蠢,我必须达到这个才能弄清楚,但可能节省了我几个小时的调试时间。
【解决方案2】:

试试这个代码....

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

【讨论】:

    【解决方案3】:

    尝试调用

    - (IBAction)computeQuiz:(id)sender 
    

    中的方法

    - (IBAction)jumpT10ResultsView:(id)sender
    

    方法而不是在按钮单击时同时调用,即使像下面的代码一样同时调用,方法 me 的调用导致没有分配 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];
    }
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          尝试:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-12-22
            • 2013-09-01
            • 1970-01-01
            • 2015-11-11
            • 2011-11-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多