Je sais qu'il y a eu quelques questions similaires à celle-ci, mais je pense que mes problèmes sont un peu différents, alors supportez-moi.Rafraîchir la tableAfficher quand l'application devient active
J'ai une application de vue à onglets avec des vues de table définies dans 2 des 3 onglets. Je souhaite pouvoir actualiser uniquement l'affichage de la table de l'onglet sélectionné lorsque l'application est réactivée. J'ai essayé d'utiliser le centre de notification pour indiquer à mon onglet de se rafraîchir, mais cela fait planter l'autre onglet parce qu'il dérobe la vue de ma progression hud. Je vais mettre du code ici, donc j'espère trouver une solution qui fonctionnera pour moi.
Tab 1 ViewController
- (void)viewDidLoad {
// becomeActive just calls viewDidAppear:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(becomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Updating";
HUD.detailsLabelText = @"Please Wait";
[HUD showWhileExecuting:@selector(refreshData) onTarget:self withObject:nil animated:YES];
}
Tab 2 ViewController
- (void)viewDidLoad
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.navigationItem.title = [defaults valueForKey:@"companyName"];
self.view.backgroundColor = background;
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = YES;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// becomeActive just calls viewDidAppear
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(becomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if (_refreshHeaderView == nil) {
EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
view.delegate = self;
[self.tableView addSubview:view];
_refreshHeaderView = view;
[view release];
}
// update the last update date
[_refreshHeaderView refreshLastUpdatedDate];
}
-(void)viewDidAppear:(BOOL)animated {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Updating";
HUD.detailsLabelText = @"Please Wait";
[HUD showWhileExecuting:@selector(updateBoard) onTarget:self withObject:nil animated:YES];
// update the last update date
[_refreshHeaderView refreshLastUpdatedDate];
}
Avec cette configuration, mon application rafraîchira onglet 2 très bien, en supposant que l'onglet 2 était le dernier onglet ouvert avant fermé la application. Si je passe à l'onglet 1 quand je ferme, au moment du redémarrage de l'application, la notification appelle l'onglet 2 en premier et vole la vue sous ma progression hud, donc quand j'essaye d'appeler la méthode viewDidAppear, la vue est nulle et l'application plante . J'ai soit besoin de comprendre comment implémenter le centre de notification mieux afin qu'il ne plante pas l'autre onglet, ou une meilleure façon de rafraîchir quand l'application devient active. Dans l'attente d'une bonne réponse. Merci d'avance.
EDIT Quand je lance avec cette configuration, il avorte dans la MBProgressHUD
- (id)initWithView:(UIView *)view {
// Let's check if the view is nil (this is a common error when using the windw initializer above)
if (!view) {
[NSException raise:@"MBProgressHUDViewIsNillException"
format:@"The view used in the MBProgressHUD initializer is nil."]; // <-- Aborts on this line, so they know it can happen
}
id me = [self initWithFrame:view.bounds];
// We need to take care of rotation ourselfs if we're adding the HUD to a window
if ([view isKindOfClass:[UIWindow class]]) {
[self setTransformForCurrentOrientation:NO];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification object:nil];
return me;
}
Votre option 2 m'a donné une idée d'essayer, qui a fonctionné, mais viewWillAppear n'est pas appelée lorsque votre application revient au premier plan. Je posterai ma propre réponse pour montrer ce que j'ai fait qui a fonctionné. Merci de votre aide. –