0

我正在开发一个 iPhone 应用程序。我NSDateFormatter用来格式化日期NSString,然后我尝试将该日期字符串设置为UILabel. 我试过这段代码:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
Result: theDate:16/10/2012

但我收到一个错误:

UILabel *lbl = [[UILabel alloc]init];
lbl.text = theDate; // the error comes here

错误信息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray isEqualToString:]: unrecognized selector sent to instance 0x6b837e0'
*** First throw call stack:
4

1 回答 1

0

尝试这个,

UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 30)];
    lbl.text = theDate;
    [self.view addSubview:lbl];

这是工作

于 2012-10-16T13:31:10.073 回答