2013-03-21 62 views
2

我正在開發一個包含最新SDK的iOS應用程序。每當我的應用程序從後臺返回時,我都需要發送消息給方法

這是一個全屏應用程序。

我有一個viewWillAppear方法,必須每次應用程序來自後臺時調用。

- (void) viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [self setUpVideo]; 
} 

setUpVideo我成立AVCaptureVideoPreviewLayer,因爲我失去了視頻的時候應用來自背景回來。

正如我所讀,viewWillAppear沒有調用時,應用程序回來的背景和現在,我不知道該把代碼放在哪裏。

對此question,occulus建議使用[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doMyLayoutStuff:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];但它不適用於我。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setUpVideo:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; 
} 

有什麼建議?

+0

請出示你的代碼,你使用的通知! – Moxy 2013-03-21 10:28:20

+0

更新了問題。 – VansFannel 2013-03-21 10:29:45

+1

@DrummerB的答案是做到這一點的方法。您也可以使用UIApplicationDidBecomeActiveNotification – Moxy 2013-03-21 10:32:52

回答

4

觀察UIApplicationWillEnterForegroundNotification代替。

- (void)viewDidAppear { 
    [super viewDidAppear]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(enterForeground:) 
       name:UIApplicationWillEnterForegroundNotification 
       object:nil]; 
    // ... 
} 

- (void)enterForeground:(NSNotification *)notification { 
    // do stuff 
} 

不要從enterForeground:方法直接調用viewWillAppear:。而是將所有必需的代碼移至單獨的方法,並從viewWillAppear:enterForeground:中調用該方法。

0

嘗試張貼從AppDelegate中的 - (void)applicationDidBecomeActive:(UIApplication *)application該通知(或遵守相應的通知,這是更好)

1
applicationWillEnterForeground will trigger when app comes from background 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
} 

此外,還可以使用UIApplicationDidBecomeActiveNotification燒製一些方法

[[NSNotificationCenter defaultCenter] addObserver: self 
              selector: @selector(handleMethod:) 
               name: UIApplicationDidBecomeActiveNotification 
               object: [UIApplication sharedApplication]]; 
相關問題