2011-05-27 57 views
1

为什么我的代码显示NSLog但不更改标签文本? 我试图展示appDelegate.times,但它不工作。为什么我的代码显示NSLog,但不更改标签文本?

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

您的格式可能不正确,即'nil'。尝试把它放在'NSLog()'中。 – 2011-05-27 23:52:00

+0

什么类是'appDelegate.times'的一个实例? – 2011-05-27 23:52:16

+1

'次'是一个浮点数吗?我注意到你正在使用两个不同的'NSString'占位符。 – raidfive 2011-05-27 23:52:46

回答

1

你需要做的是这样的:

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

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

并根据您的日志,似乎“appDelegate.times”其实并不是一个浮动(%F ...)

+2

'[differenceLabel setText:'和'differenceLabel.text ='都可以。 – raidfive 2011-05-28 00:00:14

+0

你说的有点有道理,但只是尝试了代码和标签仍然没有改变:/并没有发生错误... – myles 2011-05-28 00:03:06

+0

@Jonathan Yeh它在界面生成器中链接。 – myles 2011-05-28 00:09:44

0

这应该做的伎俩:

这里是我的功能,设置标签的文本,它看起来像这样

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

希望这有助于

相关问题