我有一个带有多个闹钟的闹钟应用程序。
有一个AlarmListViewController,它是一个包含警报列表的UITableView。选择行后,会向AlarmViewController(详细视图)启动segue。除了日期之外,其他所有内容都从AlarmListViewController正确传递到AlarmViewController。
我应该提到fullDate
是NSDate上的一个类别,可以更轻松地打印NSDate。
以下是代码:
头:
AlarmViewController.h
@interface AlarmViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIDatePicker *timeSelected;
@property (nonatomic, strong) NSMutableArray *repeatSelection;
@property (nonatomic, strong) NSString *alarmID;
@end
实现:
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"];
}
}
}
答案 0 :(得分:1)
在加载视图之前,您无法设置UI元素的值,这是您在prepareForSegue中设置日期选择器日期时尝试执行的操作。相反,您需要将日期传递给aVC,然后在其viewDidAppear方法中,将日期选择器的日期设置为日期传递的日期(您需要aVC中的属性来保存该日期)。