UIpicker选择由plist控制

et_id 发布于 2019-03-09 ios 最后更新 2019-03-09 14:34 2 浏览

如果我有一个UILabel节省了一些时间,使用下面的代码保存在plist中。现在我可以加载时间,例如凌晨1点,并在故事板中另一个视图控制器中的UIPicker上显示时间(凌晨1点)。如何做到这一点? 谢谢

- (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];
}
已邀请: