18

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

4

6 回答 6

34

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

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

于 2013-11-26T20:03:18.463 回答
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];
于 2011-12-16T10:59:35.850 回答
1

试着打电话

- (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];
}
于 2012-12-06T12:49:11.457 回答
0

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

于 2013-04-04T03:29:52.293 回答
0

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

在不丢失格式的情况下更改属性 UILabel 的文本?

于 2015-02-25T13:49:43.837 回答
-1

尝试:

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

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

于 2014-03-25T15:10:51.147 回答