0

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

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;
}
4

2 に答える 2

1

ここでの主な問題は、通知が表示されるかどうかに関係なく、すべてのビューで通知が受信されることです。画面に表示されるビューのみに通知することをお勧めします

(void)applicationWillEnterForeground:(UIApplication *)application通知の代わりに、アプリケーションデリゲートのメソッドを使用してアプリケーションの変更を処理できます。

  1. 問題が画面上のタブの更新に関するものである場合は、AppDelegateでタブの変更を追跡し、アプリケーションがフォアグラウンドに入るときにそれを使用する方が、通知よりも優れたアイデアです。

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

于 2011-09-11T04:17:42.363 に答える
0

この質問に答えるのに役立つものは本当にありません。@Learnerは、最終的にこの問題を解決する方法につながるアイデアを私に与えてくれました。

私が持っているロジックはすべて機能しているので、問題は、通知イベントに応答したときに両方が呼び出されるため、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];
    }

}
于 2011-09-11T13:25:42.947 に答える