0

If i have a UILabel saved a time, saved in plist using the below codes. Now i could like to load the time e.g. 1:00am and display the time (1:00am) on a UIPicker in another view controller in the storyboard. How this can be done?

Thanks

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


}
Clarence
  • 1,951
  • 2
  • 34
  • 49
  • Can you explain a little more about your requirement. Are you asking how to create NSDate from a saved string in plist and show it in a UIDatePicker? – Anupdas Apr 11 '13 at 18:49
  • I saved it as NSString not NSDate. I have already created the plist. I just want to know how to make the UIDatePicker show the time according to NSString. Many Thanks – Clarence Apr 12 '13 at 14:23
  • Can you include the format of date string in your question. You can use create NSDate from NSString using NSDateFormatter. And then set that date using setDate:animated method of UIDatePicker. – Anupdas Apr 12 '13 at 14:51
  • I used another Time picker to set the time in another view using the code : NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; [outputFormatter setDateFormat:@"h:mm a"]; [morningtime setText:[outputFormatter stringFromDate:self.datePicker.date]]; – Clarence Apr 12 '13 at 16:38
  • Since you are using plist you can store the instance NSDate itself. Then you can use dateFormatter to form string to display that in label. That way you can save all the details of date. When you store it as string with only information of time, you can only reproduce with the accuracy of time. Since you are only using this much information you can consider using NSUserDefaults instead of plist. – Anupdas Apr 12 '13 at 20:30

0 Answers0