2013-02-26 49 views
1

應用程序返回的我的導航控制器的景色之一,我有一個方法- (void) loadAlert是得到一個網頁的內容,分析它,並把上的相關內容UITextView。該方法調用位於我的視圖控制器的viewDidAppear:部分。當視圖第一次加載時,以及視圖在導航到時全部顯示時,它工作正常。當調用一個方法來重新加載視圖從背景

我也希望這個數據加載發生在應用程序從後臺返回。由於許多職位#1和其他來源的指示,我把我的方法調用中applicationDidBecomeActive::當從後臺應用程序返回

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

然而,沒有在視圖中出現。我期望UITextView清除,UIActivityIndicatorView旋轉,而內容是從互聯網獲得的,而新的內容將被加載到UITextView

我知道該方法正在被調用,但在視圖內沒有任何事情發生。我的感覺是,這個方法被調用時視圖還沒有加載。 我在哪裏可以放置我的方法調用,以便應用程序從後臺返回時,如果此特定視圖是導航控制器中顯示的當前視圖,它實際上會刷新UI?

+0

這是錯誤的方法。當從後臺返回時,你需要'applicationWillEnterForeground:',而不是'applicationDidBecomeActive:'。 – rmaddy 2013-02-26 22:10:42

回答

5

可以使用UIApplicationWillEnterForegroundNotificationUIApplicationDidBecomeActiveNotification,這是很有效的傾聽這些通知在應用程序中的任何地方,你需要將控制器註冊到其中的一個通知,並使用你的方法來顯示你想要的任何東西。

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

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

注意

最好的辦法是使用UIApplicationWillEnterForegroundNotification,因爲當用戶來自後臺前臺它將只運行,而另一方面UIApplicationDidBecomeActiveNotification運行,每一次你的應用程序變得活躍

+1

簡單而有效。謝謝 – DGund 2013-02-26 22:23:43

+0

除了這是錯誤的通知。 「UIApplicationDidBecomeActiveNotification」被稱爲遠多於從後臺返回。不要忘記刪除觀察者,否則你的應用會在某個時候崩潰。 – rmaddy 2013-02-26 22:25:56

+0

@rmaddy我沒有使用'applicationWillEnterForeground'。我應該何時去除觀察者? – DGund 2013-02-26 22:29:21

2

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

我在哪裏可以把我的方法調用,這樣,當從 後臺應用程序的回報,如果這個特定視圖中顯示 導航控制器,目前看來,它實際上將刷新UI?

假設你有你的UINavigationController添加爲您的appdelegate的屬性,你可以使用下面的代碼在applicationWillEnterForeground調用loadAlert在topViewController。

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

謝謝你,我測試了這個,它的工作原理 – DGund 2013-02-26 22:24:55

+0

@DGund,很高興知道這一點。請使用'UIViewController * aViewController = self.navigationController.topViewController; if([aViewController isKindOfClass:[mainViewController class]]){(mainViewController *)aViewController loadAlert]; }'如果你想確保當'mainViewController'不可見時你沒有調用loadAlert。使用通知方法,您無法確定屏幕上當前顯示哪一個,如果您不希望屏幕在不可見時刷新,可能會導致意外行爲。 – iDev 2013-02-26 22:26:40

+1

這不是一個相當複雜的方法來解決這個問題嗎?讓實際的視圖控制器監聽正確的通知並採取相應的行動是否更簡潔?將這個邏輯放在應用程序委託中確實會破壞封裝。 – rmaddy 2013-02-26 22:30:18

0

我的需求與我的項目完全相同,我不得不通過通知中心繫統來處理此問題。

首先,你需要在你的應用程序進入前景啓動通知:

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

然後,你需要真正聽在你需要它這個特別的通知。

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

已經有系統爲此提供了通知:'UIApplicationWillEnterForegroundNotification'。沒有必要實現'applicationWillEnterForeground:',以便您可以發佈自定義通知。 – rmaddy 2013-02-26 22:12:23

+0

好吧,不知道。爲了解釋如何拋出自定義通知,我會讓我的答案。 – Kirualex 2013-02-26 22:23:53

2

處理正確的方法是讓你的UIApplicationWillEnterForegroundNotificationMainViewController寄存器。然後,當收到通知時,您可以撥打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負責知道應該發生什麼以及何時發生。

+0

您提到移除觀察者。我看到你在'dealloc'中做了這個,但我的應用使用ARC。我還需要嗎? – DGund 2013-02-26 22:31:19

+0

@DGund,會的dealloc即使在ARC項目被調用。只是你不需要在那裏做發佈聲明。 – iDev 2013-02-26 22:33:19

+0

是,'dealloc'是正確的地方,即使ARC。我在我的答案中假定了ARC。注意沒有調用'[super dealloc]'。 – rmaddy 2013-02-26 22:33:41

相關問題