2014-10-20 45 views
5

讓我們假設ParentViewcontroller爲P,FirstViewController爲V1,SecondViewController爲V2。viewDidAppear呼籲在iOS8中被解僱的視圖控制器

我從V1提交V1,然後從V1提交V2。現在我想直接去P.對於這個我使用

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:NO completion:nil]; 

這工作正常,在iOS7。但是在iOS8中,我這樣做時遇到了一個問題(不知道它是否是問題)。這是V1的viewDidAppear方法正在被調用,並且它的視圖在屏幕上小部分秒鐘也會出現問題。

這是iOS8功能還是錯誤?有沒有其他方法可以做到這一點?

ViewControllers呈現代碼。

從P,其中P是推視圖控制器,

ViewController1 *v1 = [[ViewController1 alloc] init];  
[self presentViewController:v1 animated:NO completion:nil]; 

從V1,

ViewController2 *v2 = [[ViewController2 alloc] init]; 
[self presentViewController:v2 animated:NO completion:nil]; 
+0

我不知道它是否會有所作爲,但dismissModalViewControllerAnimated:被折舊很久以前,您應該使用dismissViewControllerAnimated:completion :. – rdelmar 2014-10-20 15:14:40

+0

謝謝@rdelmar。這是一個複製粘貼問題。複製了一箇舊代碼。編輯我的問題:)。 – iCanCode 2014-10-21 07:11:18

+0

這看起來對我來說不是一個錯誤。這應該根據你在做什麼來預期。你如何呈現視圖控制器?張貼演示文稿代碼 – Lefteris 2014-10-21 07:38:08

回答

0

您可以使用NavigationController(NV)的V1嵌入?通過這種方式,你可以推動你的V2。 然後,您可以彈出到V1或將NV關閉到P.

+0

不!我不能。我能夠轉到P.我的問題是當我直接從V2轉到P時,V1的視角故障只有幾分之一秒。 – iCanCode 2014-10-21 10:32:30

0

花費了大量的時間在研究結束後通過修補問題。

我所做的是,在解僱ViewController(V2)之前,我捕獲了ParentViewcontroller(P)的屏幕截圖並將其添加到應用程序窗口中。然後在ViewController(V2)解散之後從窗口中移除圖像。

CGRect deviceRect = [[UIScreen mainScreen] bounds]; 

UIGraphicsBeginImageContextWithOptions(deviceRect.size, NO, [UIScreen mainScreen].scale); 
CGContextRef contextRef = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(contextRef, -deviceRect.origin.x, -deviceRect.origin.y); 
[[self.presentingViewController.presentingViewController.view layer] renderInContext:contextRef]; 
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

UIImageView *windowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, deviceRect.size.width, deviceRect.size.height)]; 
[windowImageView setImage:capturedImage]; 
[[[UIApplication sharedApplication] keyWindow] addSubview:windowImageView]; 

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:NO completion:^ 
{ 
     [windowImageView removeFromSuperview]; 
}]; 
0

那viewWill/DidAppear'是當你解僱V1是符合邏輯,因爲技術上V1在頂部的viewController(V2)的模式解僱暴露所援引的事實,如果只是爲了足夠長的UIKit然後獲取任務去除下一個底層viewController(例如V1)。

我的猜測是iOS8中的V1視覺故障並不是一個錯誤,更多的是與iOS解決iOS8中模態內模式有關的問題。由於沒有文檔存在這種情況下,其實際行爲可以合法地從iOS更改爲iOS。我想一種方法來做你想做的事情,保持向後兼容性到老的iOS,並使用API​​的記錄行爲,將使用下面的代碼(也許作爲UIViewController的一個類別)。使V1變爲動畫,然後簡單地將其移除(下面的#2)。 V2可能使用標準的iOS'presentViewController'在V1內部動畫,但你也可以使用方法#1。

請注意,由於您將V1.view作爲子視圖添加到P而不是通過UIViewController模式系統,因此您將失去V1中的自動化viewWill/DidAppear調用。所以你需要手動調用這些。在這個例子中,它們是在'animate up'類別方法中調用的。

這不是我實際使用的代碼,但我在過去做過類似的事情。所以要小心一點,如果發現錯誤請發表評論....承擔ARC:

希望這有助於。

//1. animate V1 up (executed from P) 
    ViewController * V1 = [[ViewController1 alloc] init]; 
    //place V1 view below bottom of P 
    V1.view.frame = CGRectMake(0, UI_phone_height, UI_phone_width, UI_phone_height); 
    [self.view addSubview: V1.view]; //'self' is P 
    [V1 customAnimateUpCategory]; 

    // >> method #1 in a UIViewController category 
    - (void) customAnimateUpCategory { 

      //...optional 
      [self viewWillAppear]; 

      [UIView animateWithDuration:0.4 
        animations:^ { 
         //animate V1.view up 
         self.view.frame = CGRectMake(0, 0, UI_phone_width, UI_phone_height); 
        } 
        completion:^ (BOOL finished) { 
         if (finished) { 
          //...optional 
          [self viewDidAppear]; 
         } 
        } 
      ]; 
    } 


    //2. make V1/V2 disappear without animation (executed from V2) 
    //'V1' is weakly held in V2 as an IVAR 
    [self.V1 removeViewCategory] 

    // >> method #2 in a UIViewController category 
    - (void) removeViewCategory { 
      [self.view removeFromSuperview];//should remove and release V1/V2 glitch free!! 
    } 
相關問題