为什么我的代码显示NSLog,但不更改标签文本?
问题描述:
为什么我的代码显示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);
}
答
你需要做的是这样的:
[differenceLabel setText:[NSString stringWithFormat:@"%@", appDelegate.times]];
你真的不需要自己为一个新的NSString对象实例化......而且,你忘了你的释放NSString的对象......
并根据您的日志,似乎“appDelegate.times”其实并不是一个浮动(%F ...)
答
这应该做的伎俩:
这里是我的功能,设置标签的文本,它看起来像这样
-(void)seeValue { appdelegate = (*QueriesAppDelegate*)[[UIApplication sharedApplication]delegate]; lbl.text = [NSString stringWithFormat:@"%.f",appdelegate.f]; }
我为我的标签LBL的文本指定一个浮点值,这里是目前的代码视图我的appdelegate文件
内@interface *QueriesAppDelegate : NSObject { float f; } @property (nonatomic,assign) float f; @property (nonatomic, retain) IBOutlet UIWindow *window; @end
,这里是为了我的appdelegate.m文件
@implementation *QueriesAppDelegate @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; }
希望这有助于
您的格式可能不正确,即'nil'。尝试把它放在'NSLog()'中。 – 2011-05-27 23:52:00
什么类是'appDelegate.times'的一个实例? – 2011-05-27 23:52:16
'次'是一个浮点数吗?我注意到你正在使用两个不同的'NSString'占位符。 – raidfive 2011-05-27 23:52:46