【问题标题】:Iphone XCode My Countdown no longer AnimatesIphone XCode 我的倒计时不再动画
【发布时间】:2011-04-03 22:09:27
【问题描述】:

大家好,还有一个问题!

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

我构建了一个倒计时应用程序,它显示广播电台何时开始直播,倒计时运行完美,并且每秒实时更新一次,然后我将其插入到“If 语句”中,这样​​当站发射到达倒计时消失了,但现在我的倒计时没有实时更新,它只是静止不动。

以下是我的代码:

- (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)

【问题讨论】:

  • 包含updateLabel 的代码。您只在您希望标签每秒更新一次的地方设置标签的文本一次。
  • 请修正格式。看来您已将设置标签文本(现在已注释掉)的代码移至 viewDidAppear,它仅在显示整个视图时调用。我认为您需要花一些时间阅读以下developer.apple.com/iphone/library/featuredarticles/…

标签: iphone xcode sdk countdown


【解决方案1】:

我发现 NSLog() 对于追踪不能正确触发条件的东西非常有用。

在你的if()之前,去:

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

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

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

此外,您几乎泄漏了您在该代码中分配的所有内容。请返回release alloc 的所有内容!

【讨论】:

  • 将此添加到应用程序并在控制台中运行,但没有任何内容添加到任何类型的日志中,我之前运行过 NSLogs,所以我知道它们应该如何显示,但这只是没有显示任何东西
  • 你知道吗,我说要使用 %d,它应该是 %f,因为 NSTimeInterval 是一个 double。我很惊讶你没有在那里得到 BAD_EXEC... 用 %f 试试看它是否说明了任何合理的内容。
猜你喜欢
  • 2014-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多