popToRootViewController后没有调用viewWillAppear方法

时间:2012-08-26 02:52:37

标签: ios viewwillappear poptoviewcontroller

我有一个视图控制器,即UINavigationController,它在某个时刻推送(navigationcontroller pushViewController: animated:)第二个视图,后来推送第三个视图,其中我有一个弹出回根的按钮查看(popToRootViewController: animated:)。问题是在视图回到根目录后,根视图的方法viewWillApper没有被调用。我设置了一些断点来检查它,它只是没有通过它。我有一个方法来重新加载放置在viewWillApper中的根视图的一些内容,并在popToRootViewController: animated之后完全传递。 知道发生了什么事吗?

由于

2 个答案:

答案 0 :(得分: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];
}

我并不是说这是最好的解决方案,但它对我来说很有用!

答案 1 :(得分:0)

使用navController进行推送时,假设VC1正在推送VC2,并且您正在使用自定义表示样式来推送VC2,则取决于您选择哪种样式,当VC2弹出时不会调用VC1的viewWillAppear。以下是根据其表示方式调用when的列表。

UIModalPresentationStyle,iPhone,iPad

.fullScreen是是
.pageSheet是否
.formSheet是否
.currentContext是是
.custom否否
.overFullScreen否否
.overCurrentContext否否
.blurOverFullScreen仅在tvOS上-不适用,不适用
.popover是否
.none CRASH CRASH

reference found here