【问题标题】:Changing button text is not working iOS更改按钮文本不起作用 iOS
【发布时间】:2013-12-23 08:48:46
【问题描述】:

我已经使用故事板拖动了一个按钮,并在 .h 文件中为其创建了 IBoutlet 并合成了它。

现在我正在尝试更改按钮的文本,但它对我不起作用。

- (id)initWithCoder:(NSCoder *)decoder
{
    if ((self = [super initWithCoder:decoder])) 
    {
        configDetails *updatedConfig=[[configDetails alloc]init];
        [updatedConfig setAllItemstext:@" Go to AllItems"];
        [updatedConfig setAboutUstext:@"Text for AboutUs button"];
        [updatedConfig setHomeScreenTitleColor:@"Yellow"];

        NSString *newtext=[updatedConfig aboutUstext];
        NSLog(@"%@",newtext);
        [aboutUs setTitle:[updatedConfig aboutUstext] forState:UIControlStateNormal];

        [allItems setTitle:[updatedConfig allItemstext] forState:UIControlStateNormal];
    }

return self;
}

【问题讨论】:

  • 上面的代码真的在执行吗?您是否尝试过添加断点?
  • [updatedConfig allItemstext] 获得价值了吗?
  • @AnoopVaidya ya 它打印给定的文本
  • @Chancy 我试过没用
  • 尝试在viewDidLoad中设置标题

标签: ios objective-c uibutton storyboard


【解决方案1】:

尝试像这样将您的代码写入viewDidAppear:-

-(void)viewDidAppear:(BOOL)animated{
  configDetails *updatedConfig=[[configDetails alloc]init];
    [updatedConfig setAllItemstext:@" Go to AllItems"];
    [updatedConfig setAboutUstext:@"Text for AboutUs button"];
    [updatedConfig setHomeScreenTitleColor:@"Yellow"];

    NSString *newtext=[updatedConfig aboutUstext];
    NSLog(@"%@",newtext);
    [aboutUs setTitle:[updatedConfig aboutUstext] forState:UIControlStateNormal];

    [allItems setTitle:[updatedConfig allItemstext] forState:UIControlStateNormal];
[super viewDidAppear:animated];
}

【讨论】:

    【解决方案2】:

    您不能在 initWithCoder 方法中设置您的 IBOutlet 属性。

    请参阅我的answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 2013-05-17
      • 2012-06-03
      • 1970-01-01
      • 2020-02-13
      • 2013-07-17
      • 1970-01-01
      相关资源
      最近更新 更多