Iphone XCode我的倒计时不再是动画

时间:2010-08-25 19:19:52

标签: iphone xcode sdk countdown

嘿,是另一个问题!

我再次声明我是新手,因此我理解语言Objective C和Xcode应用程序的能力有限!所以非常感谢你的帮助!

我建立了一个倒计时应用程序,显示一个广播电台何时上线,倒计时工作完美并每秒实时更新一次,然后我将其插入“If-Statement”,以便当电台启动到达倒计时消失,但现在我的倒计时没有实时更新它只是静止。

以下是我的代码:

- (void)viewDidAppear:(BOOL)animated {

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


        if ([today timeIntervalSinceDate:currentDate] <= 0 ){   
                    self.today = [NSDate date];
        NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
        countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", components.day, components.hour, components.minute, components.second ];
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
        [self updateLabel]; 
        days.text = @"Days";
        hours.text = @"Hours";
        mins.text = @"Mins";
        secs.text = @"Secs";
        until.text = @"Until XtremeFM Launch!";
    }

    else {
        countdownLabel.text =@"XtremeFM is LIVE";

    }
}

任何帮助都非常感谢,我真的希望有人可以帮助我!

谢谢你们, 菲尔

这里是主屏幕的整个.M文件的代码

#import "XtremeFMViewController.h"

@implementation XtremeFMViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



@synthesize countdownLabel, today, timer, todayDate, days, hours, mins, secs, until;


-(void)updateLabel {
    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];


}

    - (void)viewDidAppear:(BOOL)animated {

        self.today = [NSDate date];
        NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"MM/dd/yyyy hh:mm:ss"];

        NSString *fathersDay = [[NSString alloc] initWithString:@"11/26/2010 11:59:59"];
        NSDate *currentDate = [dateformatter dateFromString:fathersDay];

        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        NSDateComponents *components = [gregorian components:unitFlags fromDate:today toDate:currentDate options:0];


        if ([today timeIntervalSinceDate:currentDate] <= 0 ){   
                    self.today = [NSDate date];
        NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
        countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", components.day, components.hour, components.minute, components.second ];
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel) userInfo:nil repeats:YES];
        [self updateLabel]; 
        days.text = @"Days";
        hours.text = @"Hours";
        mins.text = @"Mins";
        secs.text = @"Secs";
        until.text = @"Until XtremeFM Launch!";
    }

    else {
        countdownLabel.text =@"XtremeFM is LIVE";

    }
}


- (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:00"];
    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 timeIntervalSinceDate:currentDate] >= 0 ){

        NSString *webpage = @"http://xphiltestpagex.is.livestream-api.com/livestreamiphone/philtestpage/playlist.m3u8";
        NSURL *nswebpage = [NSURL URLWithString:webpage];
        NSURLRequest *nsurl = [NSURLRequest requestWithURL:nswebpage];
        [webView loadRequest:nsurl];

    }
    else {

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


}

-(IBAction)requestsPage {
    RequestsPageViewController *move = [[RequestsPageViewController alloc]initWithNibName:nil bundle:nil];
    move.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:move animated:YES];
    [move release];
}

-(IBAction)twitterButton {
    RequestsPageViewController *move = [[RequestsPageViewController alloc]initWithNibName:nil bundle:nil];
    move.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:move animated:YES];
    [move release];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

看到ViewDidAppear上有一个(BOOL)动画,但我不知道如何将其添加到if语句中(Noob)

1 个答案:

答案 0 :(得分:0)

我发现NSLog()非常有价值,可以追踪那些没有正确触发条件的东西。

if()之前,请转到:

NSLog(@"%d", [today timeIntervalSinceDate:currentDate]);

然后看看你在控制台上得到了什么。

我搞砸了这种事情的一个地方是以错误的顺序测试值,所以我期望的是负面的是积极的,反之亦然。

此外,您几乎泄漏了在该代码中分配的所有内容。回过头来release alloc取悦你所有的一切!

相关问题