0

如果我有一个 UILabel 保存时间,使用下面的代码保存在 plist 中。现在我想加载时间,例如凌晨 1:00 并在情节提要的另一个视图控制器中的 UIPicker 上显示时间(凌晨 1:00)。如何做到这一点?

谢谢

- (void)viewDidLoad
{NSString *myPath = [self saveFilePath];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];

    if (fileExists)
    {

        NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
        morningtime.text = [values objectAtIndex:0];
        afternoontime.text = [values objectAtIndex:1];

    }

    UIApplication *myApp = [UIApplication sharedApplication];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(applicationDidEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:myApp];

    [super viewDidLoad];       }
- (NSString *) saveFilePath
{
    NSArray *path =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[path objectAtIndex:0] stringByAppendingPathComponent:@"savefile.plist"];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSArray *values = [[NSArray alloc] initWithObjects:morningtime.text,afternoontime.text,nil];
    [values writeToFile:[self saveFilePath] atomically:YES];


}
4

0 回答 0