0

我的项目是一个带有标签栏控制器和 3 个视图控制器的窗口。第一个视图控制器有一个日期选择器。我使用当天 ( [NSDate date]) 作为选择器的最大限制。

问题是我无法使用较新的日期刷新该选择器(例如,假设我在几天后运行该应用程序)。

在研究了这个论坛之后,我发现我必须在这个方法中进行刷新(在委托文件中):- (void)applicationDidBecomeActive:(UIApplication *)application

但是我无法从那里创建对第一个视图控制器的正确引用。我试图使用一个指针,[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
4

2 回答 2

2

为什么要在应用程序委托中而不是在视图控制器中执行此操作?AppDelegate负责应用程序范围的任务(因此它采用了委托方法)。如果您需要在视图控制器中刷新视图,请在视图控制器中进行——这就是 MVC 设计模式的重点。

在 a 的情况下UITabBarController,我会在viewWillAppear. 在此方法中,您可以更新maximumDate属性的值。

更新:如果在重新激活应用程序时被调用viewWillAppear:viewDidAppear:似乎没有被调用,您可以在视图控制器内订阅 AppDelegate 通知。Apple 的委托范例遵循一种政策,即他们倾向于广播与委托方法匹配的通知。举个例子:有一个 NSNotification 叫做UIApplicationDidBecomeActiveNotification. 您可以通过以下方式订阅此通知viewWillAppear:

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
   [[NSNotificationCenter defaultCenter] addObserver:self 
                                            selector:@selector(applicationDidBecomeActive:) 
                                                name:UIApplicationDidBecomeActiveNotification 
                                              object:nil];
}

- (void)applicationDidBecomeActive:(NSNotification *)note {
   self.datePicker.maximumDate = [NSDate date];
}

只要确保删除这个观察者viewWillDisappear:

于 2013-01-09T23:27:11.740 回答
-1

在您的 PPPViewController 头文件和实现文件中,创建一个名为 - (void)refreshPicker;

在您实施此方法时,请像这样设置您的 dumPicker 的 maximumDate:

- (void)refreshPicker
{
    [self.dumPicker setMaximumDate:[NSDate date]];
}

使用通知。在 applicationDidBecomeActive 方法中,可以调用

[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshPicker" object:nil];

在您的 PPPViewController 的 viewDidLoad 方法中,您可以通过添加以下代码行来添加观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshPicker) object:nil];
于 2013-01-09T22:15:05.503 回答