2011-12-03 34 views
0

我有一個使用我的showNewView方法通過我的主UIViewController(稱爲MainUIVC)加載的UIViewController(稱爲SubUIVC),當我完成SubUIVC時,使用goBack返回MainUIVC方法。如何在removefromsuperview後執行方法

我希望做的是在MainUIVC執行一些代碼一次GoBack方法完成,我的MainUIVC.view已經加載。

任何人都可以幫助我做到這一點?以下是我上面提到的兩種方法。

-(void) showNewView:(UIViewController*)showView{  
UIView *currentView = self.view; 

    // get the the underlying UIWindow, or the view containing the current view 
    UIView *theWindow = [currentView superview];  
    UIView *newView = showView.view; 

    // remove the current view and replace with newView 
    [theWindow addSubview:newView]; 

    // set up an animation for the transition between the views 
    CATransition *animation = [CATransition animation]; 
    [animation setDuration:WIPE_ANIMATION_DURATION]; 
    [animation setType:kCATransitionPush]; 
    [animation setSubtype:kCATransitionFromRight]; 
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 

    [[theWindow layer] addAnimation:animation forKey:@"SwitchToNewView"]; 
} 

-(IBAction)goBack:(id)sender{  
    UIView *currentView = self.view; 

    // get the the underlying UIWindow, or the view containing the current view 
    UIView *theWindow = [currentView superview]; 

    // remove the current view and replace with superview 
    [currentView removeFromSuperview]; 

    // set up an animation for the transition between the views 
    CATransition *animation = [CATransition animation]; 
    [animation setDuration:WIPE_ANIMATION_DURATION]; 
    [animation setType:kCATransitionPush]; 
    [animation setSubtype:kCATransitionFromLeft]; 
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 

    [[theWindow layer] addAnimation:animation forKey:@"SwitchToNewView"]; 
} 
+0

爲什麼不ü用persentModalView或navigationController的櫃面 - pushViewController和SubUIVC的viewDidDisappear編寫代碼。這將是顯示viewController視圖的理想方法。 – samfisher

+0

該代碼需要在MainUIVC中執行,因爲它是在SubUIVC完成其工作後對視圖的操縱 – Vince613

回答

1

在MainController中有一個BOOL屬性比通過goBack:function設置的BOOL屬性。

在主控制器的viewDidAppear :,檢查BOOL,看看是否認爲只是表現爲GoBack的結果:被調用,並做你的事。顯然你應該重置該標誌。

+0

我試過這樣做,但viewDidAppear似乎沒有在removefromsuperview之後調用。我猜這是因爲當你添加一個子視圖時,你並沒有真正擺脫視圖,只是在視圖上添加視圖,所以當你刪除上層時,視圖並不像它那樣「出現」首先加載。這可能是錯誤的語義,但實質上它不以我需要的方式行事。 – Vince613

相關問題