【问题标题】:only 1 of 2 values displayed on UILabelUILabel 上仅显示 2 个值中的 1 个
【发布时间】:2012-11-26 08:32:14
【问题描述】:

我希望我的应用程序上的 ServingSize UIlabel 显示部分和单位(如 1 fl. oz.)。但是,它只显示单位(所以我只看到 fl. oz.)。我看不出有什么问题。下面是相关代码。我在 .m 文件的顶部将部分和单元声明为 NSString*。当我分配它之后的 NSLog 部分时,我看到了正确的值。

for(NSDictionary *dict in self.team){
        portion = [dict objectForKey:@"portion"];
        unit = [dict objectForKey:@"unit"];
    }

    self.ServingSize.text = (@"%@ %@", portion, unit);
    self.Servings.backgroundColor = [UIColor whiteColor];

【问题讨论】:

  • 也许试试 NSString stringWithFormat 而不是 @"%@ %@"?
  • 是的,[NSString stringWithFormat:@"%@ %@", portion, unit] 会更好。如果这不起作用,则应检查各个字段并确保 portionunit 返回有效值。
  • 完美!有效。谢谢!

标签: objective-c ios nsstring uilabel


【解决方案1】:

使用下面的代码(我发布了这个答案,因为它可能对其他人有帮助)

or(NSDictionary *dict in self.team){
        portion = [dict objectForKey:@"portion"];
        unit = [dict objectForKey:@"unit"];
    }

    self.ServingSize.text =[NSString stringWithFormat:@"%@ %@", portion, unit];
    self.Servings.backgroundColor = [UIColor whiteColor];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多