如何解决为什么我的代码显示NSLog但不更改标签文本?
| 为什么我的代码显示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);
}
解决方法
您需要这样做:
[differenceLabel setText:[NSString stringWithFormat:@\"%@\",appDelegate.times]];
您确实不需要为此单独实例化一个新的NSString对象。此外,您还忘记了释放NSString对象。
并且根据您的日志,看来\“ appDelegate.times \”实际上不是浮点数(%f ...)
, 这应该可以解决问题:
这是我的功能,它设置标签文本,看起来像这样
-(无效)seeValue
{
appdelegate =(stackoverflowQueriesAppDelegate *)[[UIApplication sharedApplication] delegate];
lbl.text = [NSString stringWithFormat:@ \“%。f \”,appdelegate.f];
}
我将浮点值分配为标签lbl的文本,这是我的appdelegate文件中存在的代码的视图
@interface stackoverflowQueriesAppDelegate:NSObject {
浮动f;
}
@property(nonatomic,assign)float f;
@属性(非原子的,保留)IBOutlet UIWindow *窗口;
@结束
这是我的appdelegate.m文件的视图
@implementation stackoverflowQueriesAppDelegate
@synthesize window = _window,f;
-(BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//应用程序启动后进行自定义的替代点。
f = 225.32;
myview * obj = [[myview alloc] init];
[self.window addSubview:obj.view];
[self.window makeKeyAndVisible];
返回是;
}
希望这可以帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。