iPhone XCode 创建与日期相关的 if 语句
所以我对开发事情很陌生,这让我成为一个菜鸟。 我目前正在创建我的第一个 iPhone 应用程序,其中有一个与网络视频服务启动时间相关的倒计时。 在倒计时下,我有一个允许播放视频流的按钮,我想创建一个 if 语句,告诉应用程序引用 iPhone 当前日期,如果超过了启动日期,那么 iPhone 将启动视频流,如果用户在启动日期之前按下按钮我希望出现一个 UIAlert 视图来显示服务启动的日期,到目前为止我已经有了这个。
- (IBAction)watchLive {
self.today = [NSDate date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
NSString *launchDay = [[NSString alloc] initWithString:@"11/26/2010 11:59:59"];
NSDate *currentDate = [dateformatter dateFromString:launchDay];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:today toDate:currentDate options:0];
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", components.day, components.hour, components.minute, components.second ];
if (today >= currentDate){
UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"Coming Soon" message:@"Feed goes live Friday 26th November 12:00pm" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
}
else {
UIAlertView*alert =[[UIAlertView alloc] initWithTitle:@"Perfect" message:@"It Works" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
非常感谢任何帮助,因为我真的不确定我是怎么出错的或者我应该做什么。
哦,是的最后一件事,当我在模拟器和设备上运行该应用程序时,它只是随机显示一条消息,我无法确定导致每条消息显示的原因,但它不是日期!
谢谢你们! 菲尔
So I am new to the development thing, making me yes a noob.
I am currently creating my first iPhone app and in it I have a countdown relating to when a web video service is launching.
Under the countdown I have a button that allows playing the video stream, I would like to create an if statement that tells the app to reference the iPhone current date and if it is over the launch date then the iPhone will launch the video stream, if the user presses the button before the launch date I would like a UIAlert View to appear saying the date of the service launch, so far I have this.
- (IBAction)watchLive {
self.today = [NSDate date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
NSString *launchDay = [[NSString alloc] initWithString:@"11/26/2010 11:59:59"];
NSDate *currentDate = [dateformatter dateFromString:launchDay];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:today toDate:currentDate options:0];
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", components.day, components.hour, components.minute, components.second ];
if (today >= currentDate){
UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"Coming Soon" message:@"Feed goes live Friday 26th November 12:00pm" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
}
else {
UIAlertView*alert =[[UIAlertView alloc] initWithTitle:@"Perfect" message:@"It Works" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Any help is greatly appreciated as I really am not sure how I am going wrong or what i should have in place.
Oh yes one last thing, when I run the App in the simulator and on device, it just randomly displays one of the messages, and I cannot determine what causes each message to display but it isn't the date!
Thanks guys!
Phil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
比较日期时,您应该使用
NSDate
的比较方法您看到的行为是比较今天和 currentDate 的内存地址的结果,
请
参阅
NSDate 类参考了解更多详细信息
When comparing dates you should use the comparison methods of
NSDate
The behaviour you are seeing is a result of comparing the memory addresses of today and currentDate
Change
To
Refer to the NSDate Class Reference for more detail