0

我知道有几个问题与此类似,但我认为我的问题有点不同,请耐心等待。当应用程序变为活动状态时刷新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; 
} 

回答

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

} 
1

主要问题是内部的通知是由所有收到的意见,他们是否会出现与否。好的做法是仅通知那些将出现在屏幕上的视图

您可以使用应用程序代理的(void)applicationWillEnterForeground:(UIApplication *)application方法来处理应用程序中的更改而不是通知。

  1. 如果问题是所有有关刷新标签是在屏幕上,然后跟踪在AppDelegate的标签变化,并使用该应用程序时,进入前景将超过通知更好的主意。

  2. 其他选项是在tabbarcontroller中使用viewWillAppear方法。当调用此方法时,可以刷新当前选项卡。

+0

您的选择2给了我一个想法,尝试,工作,但viewWillAppear不会调用时,您的应用程序返回到前台。我会张贴我自己的回答,以显示我所做的工作。感谢您的帮助。 –

相关问题