OGeek|极客世界-中国程序员成长平台

标题: ios - Cocoa touch如何将标签设置为NSDate [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:08
标题: ios - Cocoa touch如何将标签设置为NSDate

我有一个名为 datelabel 的标签,我想在我的 AppDelgate 中将它设置为一个名为 dateofbirth 的 NSDate。

我已经定义并导入了我的 AppDelgate 并将其设置为 appDelgate,但是当我尝试 self.datelabel.text = appDelgate.dateofbirth

但是当我把它放进去时,我的这个错误 Incompatible pointer types assignmenting to 'NSString *' from 'NSDate *'

我尝试了一些方法,但都没有奏效。

有没有办法为 NSDate 设置标签。



Best Answer-推荐答案


您需要将 NSDate 转换为 NSString。您可以为此使用 NSDateFormatter

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// you can use one of the builtin localizated formats
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
self.datelabel.text = [formatter stringFromDate:appDelgate.dateofbirth];
// or you can set your own format
[formatter setDateFormat"yyyy-MM-dd"];
self.datelabel.text = [formatter stringFromDate:appDelgate.dateofbirth];

关于ios - Cocoa touch如何将标签设置为NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19745006/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4