簡體   English   中英

在哪里調用從后台返回應用時重新加載視圖的方法

[英]Where to call a method to reload view on app return from background

在導航控制器的其中一個視圖中,我有一個方法- (void) loadAlert ,該方法獲取網頁的內容,對其進行解析,然后將相關內容放置在UITextView 方法調用位於我的視圖控制器的viewDidAppear:部分。 在首次加載視圖時以及在導航至視圖時根本不顯示視圖時,它都可以正常工作。

我還希望當應用程序從后台返回時發生這種數據加載。 正如許多Stackoverflow帖子和其他來源指出的那樣,我將我的方法調用放在applicationDidBecomeActive:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    MainViewController *mainView = [[mainViewController alloc] init];
    [mainView loadAlert];
}

但是,當應用程序從后台返回時,視圖中什么也沒有發生。 我希望在從Internet獲得內容時清除UITextView ,使UIActivityIndicatorView旋轉,並將新內容加載到UITextView

我知道該方法正在被調用,但是視圖內什么也沒有發生。 我的感覺是,在調用此方法時,視圖尚未加載。 我在哪里可以放置我的方法調用,以便當應用程序從后台返回時,並且如果此特定視圖是導航控制器中顯示的當前視圖,它將實際上刷新UI?

您可以使用UIApplicationWillEnterForegroundNotificationUIApplicationDidBecomeActiveNotification ,在應用程序中的任何位置偵聽這些通知是非常有效的,您需要將控制器注冊為這些通知之一,並且僅使用您的方法顯示所需的內容。

     [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(showYourView)
                                                 name:UIApplicationWillEnterForegroundNotification object:nil];

-(void) showYouView(){
 // do your stuff
}

注意

最好的方法是使用UIApplicationWillEnterForegroundNotification,因為它僅在用戶從后台進入前台時才運行,另一方面,UIApplicationDidBecomeActiveNotification每次在您的應用程序處於活動狀態時運行

您正在創建mainViewController的新實例,而不是使用已經存在的相同實例。 每當您執行mainViewController *mainView = [[mainViewController alloc] init]; ,這是一個新實例。 您應該嘗試訪問viewcontrollers堆棧中存在的相同實例,然后在該實例上調用此方法。

我在哪里可以放置我的方法調用,以便當應用程序從后台返回時,並且如果此特定視圖是導航控制器中顯示的當前視圖,它將實際上刷新UI?

假設您已將UINavigationController添加為appdelegate中的屬性,則可以在applicationWillEnterForeground使用以下代碼在topViewController上調用loadAlert。

UIViewController *aViewController = self.navigationController.topViewController;
if ([aViewController isKindOfClass:[mainViewController class]]) {
    [(mainViewController *)aViewController loadAlert];
}

另外,請以大寫字母開頭課程名稱。 例如:-您應該使用MainViewController代替通常表示變量名稱的mainViewController

更新:

如果使用的是UIApplicationWillEnterForegroundNotification方法,則應在notificationMethod方法中添加以上代碼,以確保mainViewController是可見的當前視圖控制器。 否則,它可能會顯示一些意外的行為。

所以像這樣的東西應該很好:

- (void)notificationMethod {

   UIViewController *aViewController = self.navigationController.topViewController;
   if ([aViewController isKindOfClass:[mainViewController class]]) {
      [(mainViewController *)aViewController loadAlert];
   }
}

解決此問題的正確方法是讓MainViewController注冊UIApplicationWillEnterForegroundNotification 然后,當收到通知時,您調用loadAlert方法。

在MainViewController.m中:

- (void)enterForeground {
    [self loadAlert];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}

您無需為此在應用程序委托中做任何事情。 這樣,MainViewController負責知道應該發生什么以及何時發生。

我對我的一個項目的需求完全相同,因此我必須通過NotificationCenter系統來處理此事。

首先,您需要在應用程序進入前台時啟動通知:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"app_entering_foreground" object:self];
}

然后,您需要在需要的地方實際收聽此特定通知。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI) @"app_entering_foreground" object:nil];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM