2011-04-16 126 views
2

當應用程序進入background.i時,我需要自動關閉我的uiimagepicker模式viewcontroller。我嘗試將代碼放在viewdiddissappear方法中的dismissmodalviewcontroller代碼中,但它沒有被調用。所以我做了一個參考的appdelegate的視圖 - 控制,並試圖把它的applicationdidenterbackgroundmethod但仍沒有working.can有人指出,要做到這一點當應用程序進入後臺時關閉modalviewcontroller

+0

刪除觀察者怎麼樣,當應用程序再次進入前臺關閉對話框? – Till 2011-04-16 09:43:21

+0

如果有可能,它將高清與appdidenterbackground本身 – sujith1406 2011-04-16 10:06:04

+0

這是相同的,如果你把解僱代碼viewWillDisappear的viewController呈現uiimagepicker模態? – Sefran2 2011-04-16 12:10:38

回答

7

嘗試在您想要的UIViewController中添加NSNotificationCenter觀察員UIApplicationDidEnterBackgroundNotification時的正確方法解僱。使用選擇器關閉模態視圖

- (void)viewWillAppear:(BOOL)animated 
{ 
    [[NSNotificationCenter defaultCenter] addObserver: self 
             selector: @selector(didEnterBackground:) 
              name:UIApplicationDidEnterBackgroundNotification 
              object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver: self 
              name:UIApplicationDidEnterBackgroundNotification 
              object:nil]; 
} 

- (void)didEnterBackground:(NSNotification*)note 
{ 
    [self.navigationController dismissModalViewAnimated:NO]; 
} 
1

我不認爲你需要經歷所有這些。

the docs

If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.

嘗試調用從父視圖控制器[self dismissModalViewController:NO]在實施- (void) viewDidUnload

這是未經測試的,但文檔暗示它應該爲您完成這項工作。

+2

測試第一個模式視圖控制器堆棧,完美工作。 – 2013-02-01 00:08:56

2

當應用程序移動到背景並且工作正常時,最好的方法是刪除模式。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

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

- (void)dismissView:(id)sender { 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (void)dealloc { 

    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

你也可以像這樣

[[NSNotificationCenter defaultCenter] removeObserver: self 
              name:UIApplicationDidEnterBackgroundNotification 
              object:nil]; 
相關問題