我知道有几个问题与此类似,但我认为我的问题有点不同,请耐心等待。当应用程序变为活动状态时刷新tableView
我有一个选项卡式视图应用程序,其中3个选项卡中的2个设置了表视图。我希望能够在应用程序醒来时刷新选定选项卡的表格视图。我曾尝试使用通知中心来告诉我的标签刷新,但它使另一个标签崩溃,因为它正在窃取我进度hud中的视图。我会在这里提供一些代码,希望我能找到一个适合我的解决方案。
标签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];
}
标签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];
}
有了这个设置,我的申请将刷新选项卡就好了,假设选项卡是最后一个标签中打开你关闭之前应用。如果我在关闭时切换到选项卡1,则在重新启动应用程序时,通知首先调用标签2,并从我的进度hud下窃取视图,因此当我尝试调用viewDidAppear方法时,视图为空,应用程序崩溃。我需要弄清楚如何更好地实现通知中心,使其不会崩溃其他选项卡,或总体上更好的方式来刷新应用程序激活时的状态。期待一个好的答案。提前致谢。
编辑 当我在此设置运行,在这里中止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;
}
您的选择2给了我一个想法,尝试,工作,但viewWillAppear不会调用时,您的应用程序返回到前台。我会张贴我自己的回答,以显示我所做的工作。感谢您的帮助。 –