I have an alarm app with multiple alarms.
There is an AlarmListViewController which is a UITableView that holds the list of alarms. When the row is selected, a segue is initiated to an AlarmViewController (the detail view). Everything else is passed correctly from the AlarmListViewController to the AlarmViewController except the date.
I should mentioned that fullDate is a category on NSDate to make printing of NSDates easier.
Here’s the code:
header:
AlarmViewController.h
@interface AlarmViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIDatePicker *timeSelected;
@property (nonatomic, strong) NSMutableArray *repeatSelection;
@property (nonatomic, strong) NSString *alarmID;
@end
implementation:
AlarmViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"---VIEW DID LOAD---, current date: %@", [[NSDate date] fullDate]);
NSLog(@"alarmID: %@", self.alarmID);
NSLog(@"repeat: %@", self.repeatSelection);
NSLog(@"time: %@", [self.timeSelected.date fullDate]);
}
prepareForSegue:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"self.selectedCell: %d", self.selectedCell);
if ([segue.identifier isEqualToString:@"alarmDetail"] || [segue.identifier isEqualToString:@"addAlarm"])
{
AlarmViewController *aVC = (AlarmViewController *)[segue destinationViewController];
if (self.tableView.editing)
{
NSDictionary *setting = [self.alarmList objectAtIndex:self.selectedCell];
NSLog(@"setting.time = %@", [[setting objectForKey:@"time"] fullDate]);
aVC.repeatSelection = [setting objectForKey:@"repeat"];
aVC.alarmID = [setting objectForKey:@"alarmID"];
aVC.timeSelected.date = [setting objectForKey:@"time"];
}
}
}
You can’t set a UI element’s value before its view has been loaded, which is what you’re trying to do when you set the date picker’s date from prepareForSegue. Instead, you need to pass the date to aVC, and then in its viewDidAppear method, set the date picker’s date with that passed in date (you’ll need a property in aVC to hold that date).