1

我是开发 iOS 应用程序的新手,英语不是我的母语,所以请原谅任何错误和我丑陋的代码。

我正在尝试创建的应用程序应该只在特定的一天显示一张特定的图像(如果日期发生变化,请更改图像)。因此,我实现了一个无限循环,在其中检查日期。如果它与上次更改的图像不同,则图像再次更改。图像以“YearMonthDay.png”-方案命名(例如“20131017.png”)。

我已经用谷歌搜索了很多代码(我知道这很丑),但每次都会崩溃。

我真的很感激任何帮助!

smViewController.h:

#import <UIKit/UIKit.h>

@interface smViewController : UIViewController {
  UIImageView* mImageView;
}

@property (nonatomic, retain) IBOutlet UIImageView* imageView;

- (IBAction)contentModeChanged:(UISegmentedControl*)segmentedControl;

@end

smViewController.m

#import "smViewController.h"

@interface smViewController ()

@end

@implementation smViewController
@synthesize imageView = mImageView;

- (void)viewDidUnload
{
    self.imageView = nil;
    [super viewDidUnload];
}

- (void)dealloc
{
    [mImageView release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *oldDateString = @"";

    while(true)
    {
        NSDate *today = [NSDate date];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy/MM/dd"];
        NSString *dateString = [dateFormat stringFromDate:today];
        NSLog(@"date: %@", dateString);
        if([dateString isEqualToString: oldDateString])
        {
        }
        else
        {
            NSAssert(self.imageView, @"self.imageView is nil. Check your IBOutlet   connections");
            UIImage* image = [UIImage imageNamed:dateString];
            NSAssert(image, @"image is nil. Check that you added the image to your bundle and that the filename above matches the name of you image.");
            self.imageView.backgroundColor = [UIColor whiteColor];
            self.imageView.clipsToBounds = YES;
            self.imageView.image = image;
            oldDateString = dateString;
        }
        [dateFormat release];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

2 回答 2

1

您应该在您的应用程序 UIApplicationDelegate 中覆盖 - (void)applicationSignificantTimeChange:(UIApplication *)application。然后,当日期更改时,您将收到一个事件,您可以删除任何循环或计时器。

于 2013-10-08T19:30:24.073 回答
0

可能是因为while循环。它阻止了您的应用程序。

您应该改用计时器,如下所示:

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkDate) userInfo:nil repeats:YES];
[timer fire];

您希望每 1 秒检查一次新日期(频率更高可能会很好),并且checkDate是检查日期并在需要时替换图像的方法。

于 2013-10-08T19:06:14.840 回答