【问题标题】:Why does my code show the NSLog but not change the label text?为什么我的代码显示 NSLog 但不更改标签文本?
【发布时间】:2011-05-28 15:41:53
【问题描述】:

为什么我的代码显示 NSLog 但没有更改标签文本? 我正在尝试显示 appDelegate.times,但它不起作用。

-(void)Dothis
{
    //retain
    appDelegate = [[[UIApplication sharedApplication] delegate] retain];

    //display in label
    differenceLabel.text = [[NSString alloc] initWithFormat:@"%.3f", appDelegate.times];

    //display in console
    NSLog(@"Computed time wasrggsdfgd: %@", appDelegate.times);
}

【问题讨论】:

  • 您的格式可能不正确,即nil。试着把它放在NSLog()
  • appDelegate.times 是什么类的实例?
  • times 是浮点数吗?我注意到您使用了两个不同的 NSString 占位符。
  • differenceLabel 对象是否连接为 IBOutlet?
  • NSLog 工作正常并返回时间差。但是 --->[differenceLabel setText:[NSString stringWithFormat:@"%@", appDelegate.times]];

标签: iphone objective-c xcode label nslog


【解决方案1】:

你需要这样做:

[differenceLabel setText:[NSString stringWithFormat:@"%@", appDelegate.times]];

你真的不需要为此自己实例化一个新的 NSString 对象......而且你忘记释放你的 NSString 对象......

根据您的日志,“appDelegate.times”似乎实际上不是浮点数 (%f...)

【讨论】:

  • [differenceLabel setText:differenceLabel.text = 都可以工作。
  • 你说的有点道理,但只是尝试了代码和标签仍然没有改变:/并且没有发生错误......
  • @Jonathan Yeh 已在界面生成器中链接。
  • 所以如果你这样做,例如:[differenceLabel setHidden:YES];您的标签应该被隐藏。如果你这样做: [differenceLabel setText:[NSString stringWithFormat:@"BLA BLA BAL"];它应该显示“BLA BLA BLA”...如果您成功通过了这些测试,那么问题出在其他地方...
  • @Jonathan 不幸的是,我的标签不会隐藏或显示文本...有什么建议吗?
【解决方案2】:

这应该可以解决问题:

这是我设置标签文本的函数,它看起来像这样

-(void)seeValue { appdelegate = (stackoverflowQueriesAppDelegate*)[[UIApplication sharedApplication]delegate]; lbl.text = [NSString stringWithFormat:@"%.f",appdelegate.f]; }

我正在分配一个浮点值作为我的标签 lbl 的文本,这是我的 appdelegate 文件中存在的代码的视图

@interface stackoverflowQueriesAppDelegate : NSObject { 浮动 f; } @property (nonatomic,assign) float f; @property (nonatomic, 保留) IBOutlet UIWindow *window; @结尾

这是我的 appdelegate.m 文件的视图

@implementation stackoverflowQueriesAppDelegate @synthesize 窗口=_window,f; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 应用程序启动后自定义的覆盖点。 f = 225.32; myview *obj = [[myview alloc]init]; [self.window addSubview:obj.view]; [self.window makeKeyAndVisible]; 返回是; }

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多