2012-08-26 73 views
1

我有一個視圖控制器,這是一個UINavigationController,在某些時候推動(navigationcontroller pushViewController: animated:)第二個視圖,後來推第三個視圖,我有一個按鈕,彈出回到根視圖(popToRootViewController: animated:)。問題是在視圖回到根目錄之後,根視圖的方法viewWillApper未被調用。我已經設置了一些斷點來檢查它,它只是沒有通過它。我有一種方法來重新加載我的根視圖中的一些內容,放置在viewWillApper中,並在popToRootViewController: animated之後完全通過。 想知道發生了什麼?viewWillAppear方法沒有被調用後popToRootViewController

謝謝

+0

您是否將各種類型的視圖控制器嵌套在一起?顯然這不支持... http://stackoverflow.com/questions/6859868/popping-viewcontroller-doesnt-call-viewwillappear-when-going-back – RonLugge

回答

0

我用一個委託方法強制更新我的視圖後popToRootViewController。我的rootViewController稱爲網絡上傳類,完成後,我想重置rootViewController上的表單字段。

在網絡上傳類,我創建了一個委託協議:

@protocol MyNetworkDelegate <NSObject> 
@required 
- (void) uploadCompleted; 
@end 

@interface MyNetworkUploader : NSObject{ 
    id <MyNetworkDelegate> _delegate; 
} 

@property (nonatomic,strong) id delegate; 

//other properties here 

+(id)sharedManager; 
-(int)writeAssessments; 

@end 

在MyNetworkUploader.m:

-(int)writeAssessments{ 
    //code here to do the actual upload 
    //..... 

    //this is a non-view class so I use a global navigation controller 
    //maybe not the best form but it works for me and I get the required 
    //behaviour 
    [globalNav popToRootViewControllerAnimated:NO]; 
    [[globalNav.view viewWithTag:1] removeFromSuperview]; 
    [_delegate uploadCompleted]; 
} 

然後,在我的RootViewController的:

//my upload is done within a completion block so I know when 
//it's finished 
typedef void(^myCompletion)(BOOL); 

-(void) uploadAssessment:(myCompletion) compblock{ 
    //do the upload 
    sharedManager=[MyNetwork sharedManager]; //create my instance 
    sharedManager.delegate=self; //set my rootViewController as the network class delegate 
    int numWritten= [sharedManager writeAssessments]; 
    compblock(YES); 
} 

#pragma mark - protocol delegate 
-(void)uploadCompleted{ 
    //this is a local method that clears the form 
    [self clearTapped:nil]; 
} 

我不建議這是最好的解決方案,但它對我來說是一種享受!

相關問題