質問

これに似た質問がいくつかあったことは知っていますが、私の問題は少し違うと思いますので、私に耐えてください。

3つのタブのうち2つに設定されたテーブルビューを備えたタブ付きビューアプリがあります。アプリが覚醒したときに、選択したタブのテーブルビューだけを更新できるようになりたいです。通知センターを使用してタブを伝えてリフレッシュしてみましたが、Progress 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];

}

このセットアップにより、アプリケーションを閉じる前にタブ2が最後のタブが開いていると仮定して、アプリケーションがタブ2を正常に更新します。閉じたときにタブ1に切り替えた場合、アプリを再起動したときに、[通知]タブ2を最初に呼び出して、進行状況HUDの下からビューを盗むため、ViewDidapearメソッドを呼び出すと、ビューは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;
}
役に立ちましたか?

解決 2

この質問に答えるのを助けるために本当にあまりありません。 @Learnerは、最終的にこの問題をどのように解決したかにつながったアイデアをくれました。

私が機能しているすべてのロジックがあるため、問題は、HUDが通知イベントに応答すると呼ばれるため、HUDが他の見方から盗むのを防ぐことでした。だから私の中で -becomeActive 両方のタブのイベントでは、SelectedIndexが現在のタブの一致であるかどうかをチェックしました。魅力のように働いた。

-(void)becomeActive:(NSNotification *)notification {
    // only respond if the selected tab is our current tab
    if (self.tabBarController.selectedIndex == 1) { // just set the number to your tab index
        [self viewDidAppear:YES];
    }

}

他のヒント

ここでの主な問題は、通知がすべてのビューで受信されるかどうかにかかわらず、すべてのビューによって受信されることを示しています。良い習慣は、画面に表示されるビューのみに通知することです

アプリケーションデリゲートを使用できます (void)applicationWillEnterForeground:(UIApplication *)application 通知の代わりにアプリケーションの変更を処理する方法。

  1. 問題がすべて画面上のタブである場合、AppDelegateのタブの変更を追跡し、アプリケーションが前面に入るときにそれを使用することは、通知よりも良いアイデアを把握することです。

  2. その他のオプションは、TabbarControllerでViewWillappearメソッドを使用することです。このメソッドが呼び出されると、現在のタブを更新できます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top