gpt4 book ai didi

iPhone XCode 创建与日期相关的 if 语句

转载 作者:行者123 更新时间:2023-12-03 20:57:57 25 4
gpt4 key购买 nike

所以我对开发事情很陌生,这让我成为了一个菜鸟。我目前正在创建我的第一个 iPhone 应用程序,其中有一个与网络视频服务启动时间相关的倒计时。在倒计时下,我有一个允许播放视频流的按钮,我想创建一个 if 语句,告诉应用程序引用 iPhone 当前日期,如果超过了启动日期,那么 iPhone 将启动视频流,如果用户在启动日期之前按下按钮我希望出现一个 UIAlert View 来显示服务启动的日期,到目前为止我已经有了这个。

- (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];
}
}

非常感谢任何帮助,因为我真的不确定我是怎么出错的或者我应该做什么。

哦,是的最后一件事,当我在模拟器和设备上运行该应用程序时,它只是随机显示一条消息,我无法确定导致每条消息显示的原因,但它不是日期!

谢谢大家!菲尔

最佳答案

比较日期时,应该使用NSDate的比较方法您看到的行为是比较今天和 currentDate 的内存地址的结果

改变

if (today >= currentDate)

if ([today timeIntervalSinceDate:currentDate] <= 0 ) // today is before launch

请参阅NSDate Class Reference了解更多详情

关于iPhone XCode 创建与日期相关的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3567648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com