我知道有几个类似的问题被问到,但我认为我的问题有点不同,所以请多多包涵。
我有一个选项卡式视图应用程序,其中 3 个选项卡中的 2 个设置了表格视图。当应用程序唤醒时,我希望能够仅刷新所选选项卡的表格视图。我曾尝试使用通知中心告诉我的选项卡刷新,但它使另一个选项卡崩溃,因为它正在从我的进度面板中窃取视图。我会在这里放一些代码,所以希望我能找到一个适合我的解决方案。
选项卡 1 视图控制器
- (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 视图控制器
- (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];
}
使用此设置,我的应用程序将很好地刷新选项卡 2,假设选项卡 2 是您关闭应用程序之前打开的最后一个选项卡。如果我在关闭时切换到选项卡 1,在重新启动应用程序时,通知会首先调用选项卡 2 并从我的进度面板下窃取视图,因此当我尝试调用 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;
}