为什么我的代码显示 NSLog 但不更改标签文本?

发布于 2024-11-10 18:41:15 字数 397 浏览 9 评论 0原文

为什么我的代码显示 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);
}

Why does my code show the NSLog but not change the label text?
I'm trying to show the appDelegate.times but it's not working.

-(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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-11-17 18:41:15

你需要这样做:

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

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

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

You need to do it like this :

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

You really don't need to instantiate by a new NSString object by yourself for that... And moreover then you forgot to release your NSString object...

And according to your log , it seems that "appDelegate.times" is actually not a float (%f...)

妞丶爷亲个 2024-11-17 18:41:15

这应该可以解决问题:

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

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

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

@interface stackoverflowQueriesAppDelegate : NSObject  {

    float f;  
}
@property (nonatomic,assign) float f;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

,这是我的appdelegate.m 文件

@implementation stackoverflowQueriesAppDelegate
@synthesize window=_window,f;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    f= 225.32;
    myview *obj = [[myview alloc]init];
    [self.window addSubview:obj.view];
    [self.window makeKeyAndVisible];
    return YES;
}

希望这有帮助

This should do the trick:

here's my function which sets the label text and it looks like this

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

I am assigning a float value as the text of my label lbl and here's a view of the code present inside my appdelegate file

@interface stackoverflowQueriesAppDelegate : NSObject  {

    float f;  
}
@property (nonatomic,assign) float f;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

and here's a view to my appdelegate.m file

@implementation stackoverflowQueriesAppDelegate
@synthesize window=_window,f;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    f= 225.32;
    myview *obj = [[myview alloc]init];
    [self.window addSubview:obj.view];
    [self.window makeKeyAndVisible];
    return YES;
}

hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文