これに似た質問がいくつかあったことは知っていますが、私の問題は少し違うと思いますので、ご容赦ください。
3つのタブのうち2つにテーブルビューが設定されたタブ付きビューアプリがあります。アプリが起動したときに、選択したタブのテーブルビューだけを更新できるようにしたい。通知センターを使用してタブを更新するように指示しようとしましたが、進行状況からビューを盗んでいるため、他のタブがクラッシュします。ここにいくつかのコードを入れますので、うまくいけば私に役立つ解決策を見つけることができます。
タブ1ViewController
- (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];
}
タブ2ViewController
- (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メソッドを呼び出そうとすると、ビューがnullになり、アプリがクラッシュします。 。他のタブがクラッシュしないように通知センターをより適切に実装する方法を理解するか、アプリがアクティブになったときに更新するだけのより良い方法を理解する必要があります。良い答えを楽しみにしています。前もって感謝します。
編集 このセットアップで実行すると、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;
}