私のプロジェクトは、タブバーコントローラーと 3 つのビューコントローラーを備えたウィンドウです。最初のビューコントローラーには日付ピッカーがあります。現在の日 ( [NSDate date]
) をピッカーの上限として使用しています。
問題は、そのピッカーを新しい日付で更新できないことです (たとえば、数日後にアプリを実行しているとします)。
このフォーラムを調査した後、このメソッド (デリゲート ファイル内) で更新を行う必要があることがわかりました。- (void)applicationDidBecomeActive:(UIApplication *)application
しかし、そこから最初のView Controllerへの正しい参照を作成できませんでした。へのポインターを使用しようとしました[self.window.rootViewController.tabBarController.viewControllers objectAtIndex:0]
が、ビューコントローラーを指していないようです。
誰かがこの問題について何か考えを持っていますか?
コードは次のとおりです。
//
// PNAppDelegate.m
//
#import "PNAppDelegate.h"
#import "PPPViewController.h"
#import "IGPreviaViewController.h"
#import "IGUsgViewController.h"
@implementation PNAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
PPPViewController *pppvc = [[PPPViewController alloc] init];
IGPreviaViewController *igpvc = [[IGPreviaViewController alloc] init];
IGUsgViewController *iguvc = [[IGUsgViewController alloc] init];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:pppvc, igpvc, iguvc, nil];
[tabBarController setViewControllers:viewControllers];
[[self window] setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
PPPViewController *pppvc = [self.window.rootViewController.tabBarController.viewControllers objectAtIndex:0];
[pppvc.dumPicker setMaximumDate:[NSDate date]];
// Here I don't know how to proceed in order to refresh the picker in the first viewcontroller
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
//
// PPPViewController.m
//
#import "PPPViewController.h"
#import "IdadeGestacional.h"
@implementation PPPViewController
@synthesize dumPicker,idadeGest,dataParto,periodoParto,dum;
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:@"IG/DPP"];
UIImage *i = [UIImage imageNamed:@"calendario.png"];
// Put that image on the tab bar item
[tbi setImage:i];
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
// cria o dumPicker
dum = [self acertaData:[NSDate date]];
dumPicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 80, 0, 0)];
[dumPicker setTransform:CGAffineTransformMakeScale(0.8, 0.8)];
[dumPicker setDatePickerMode:UIDatePickerModeDate];
[dumPicker setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"pt_BR"]];
[dumPicker setMaximumDate:[NSDate date]];
NSDate *minDate = [[NSDate alloc] initWithTimeInterval:(-300*24*60*60) sinceDate:[NSDate date]];
[dumPicker setMinimumDate:minDate];
[dumPicker addTarget:self action:@selector(changeDum) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:dumPicker];
[idadeGest setText:@""];
[dataParto setText:@""];
[periodoParto setText:@""];
}
-(void)changeDum
{
// Define a DUM (data inicial)
NSDate *dataInicial = [[NSDate alloc]init];
if ([[NSTimeZone localTimeZone] isDaylightSavingTime]) {
dataInicial = [[dumPicker date] dateByAddingTimeInterval:(60*60)];
}
else {
dataInicial = [dumPicker date];
};
dum = [self acertaData:dataInicial];
// Calcula a IG
IdadeGestacional *IG = [[IdadeGestacional alloc] initWithDataInicial:dum dataFinal:[self acertaData:[NSDate date]]];
[IG calculaIG];
[IG display];
// Mostra a IG
NSString *IGLabel = @"";
if (IG.semanas)
{
if (IG.dias)
IGLabel = [NSString stringWithFormat:@"%ds%dd",IG.semanas,IG.dias];
else IGLabel = [NSString stringWithFormat:@"%ds",IG.semanas];
} else if (IG.dias) IGLabel = [NSString stringWithFormat:@"%dd",IG.dias];
[idadeGest setText:IGLabel];
// Calcula a DPP
NSDate *dpp = [[NSDate alloc] initWithTimeInterval:(280*24*60*60) sinceDate:dataInicial];
// Mostra a DPP
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy"];
[dataParto setText:[df stringFromDate:dpp]];
// Calcula o PPP
NSDate *pppi = [[NSDate alloc] initWithTimeInterval:(266*24*60*60) sinceDate:dataInicial];
NSDate *pppf = [[NSDate alloc] initWithTimeInterval:(294*24*60*60) sinceDate:dataInicial];
// Mostra o PPP
NSString *PPParto = [NSString stringWithFormat:@"%@ a %@",[df stringFromDate:pppi],[df stringFromDate:pppf]];
[periodoParto setText:PPParto];
}
-(NSDate *)acertaData:(NSDate *)dataBruta
{
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:dataBruta];
NSDate *novaData = [[NSCalendar currentCalendar] dateFromComponents:comps];
[self mostraData:novaData];
return novaData;
}
-(void)mostraData:(NSDate *)dataBruta
{
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit | NSCalendarCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:dataBruta];
NSLog(@"nova data");
NSLog(@"%d %d %d %d %d %d %@ %@",comps.year,comps.month,comps.day,comps.hour,comps.minute,comps.second,comps.timeZone.abbreviation,comps.calendar.calendarIdentifier);
NSLog(@"%@",comps.timeZone.name);
}
@end