【问题标题】:viewWillAppear method not being called after popToRootViewController在 popToRootViewController 之后未调用 viewWillAppear 方法
【发布时间】:2012-08-26 02:52:37
【问题描述】:

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

谢谢

【问题讨论】:

标签: ios viewwillappear poptoviewcontroller


【解决方案1】:

在 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];
}

我并不是说这是最好的解决方案,但它对我来说是一种享受!

【讨论】:

    【解决方案2】:

    当使用 navController 推送时,假设 VC1 正在推送 VC2,而您正在使用自定义演示样式来推送 VC2,取决于您选择的样式,VC2 弹出时不会调用 VC1 的 viewWillAppear。以下是根据其呈现风格调用的时间列表。

    UIModalPresentationStyle、iPhone、iPad

    .fullScreen 是 是
    .pageSheet 是 否
    .formSheet 是 否
    .currentContext 是 是
    .custom NO NO
    .overFullScreen 否 否
    .overCurrentContext 否 否
    .blurOverFullScreen  仅在 tvOS 上 - N/A, N/A
    .popover 是 否
    .none 崩溃崩溃

    reference found here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 2011-10-12
      • 2020-02-27
      • 1970-01-01
      相关资源
      最近更新 更多