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

发布于 2024-09-16 03:26:54 字数 1713 浏览 7 评论 0原文

所以我对开发事情很陌生,这让我成为一个菜鸟。 我目前正在创建我的第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2024-09-23 03:26:54

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

if (today >= currentDate)

参阅

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

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

if (today >= currentDate)

To

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

Refer to the NSDate Class Reference for more detail

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文