【问题标题】:ViewDidAppear not called when the modal view is dismissed关闭模式视图时未调用 ViewDidAppear
【发布时间】:2015-03-26 05:49:39
【问题描述】:

首先,我创建一个 MainViewController。然后在 MainViewController 中,我做

[self presentViewController:modalViewController animated:YES completion:nil];
modalViewController.modalPresentationStyle = UIModalPresentationFormSheet;

当我关闭 modalViewController 时, 在 iPhone(iPhone 6+ 除外)上,调用 MainViewController 的 viewDidAppear。 在 iPad 和 iPhone 6+ 上,不调用 MainViewController 的 viewDidAppear。

逻辑是在modalViewController 关闭时调用一个函数。我如何知道 modalViewController 何时被解除。

【问题讨论】:

  • 尝试使用其他一些“modalPresentationStyle”。那它有用吗?
  • 这是用户体验所要求的。我无法改变它。
  • 你在打电话吗:[super viewDidAppear:animated];
  • 是的,我打过电话。我认为问题在于,在 iPhone 上,模态视图占据了整个屏幕,而在 iPad 上,模态视图仅位于中心。
  • 我试着重现你的场景,这里是code。对我来说,它可以在所有设备上正常工作。请参考一下。希望对您有所帮助。

标签: ios objective-c iphone viewcontroller modalviewcontroller


【解决方案1】:

当您关闭模态视图控制器时,您可以使用委托在 MainViewController 中调用您的函数。例如:

MainViewController.h:

@protocol YourDelegate <NSObject>
- (void)someFunction;
@end

@interface MainViewController : UIViewController <YourDelegate>

@end

MainViewController.m:

// Where you present the modal view
ModalViewController *view = [[ModalViewController alloc] init];
view.delegate = self;
[self presentViewController:view animated:YES completion:nil];

ModalViewController.h:

@interface ModalViewController : UIViewController
@property (nonatomic, weak) id<YourDelegate> delegate;
@end

ModalViewController.m

// Wherever you dismiss..
[self dismissViewControllerAnimated:YES completion:^{
    [self.delegate someFunction];
}

【讨论】:

  • 如果项目使用storyboard+segues怎么办?我的意思是 segue 在故事板中定义的情况,而不是在代码中
【解决方案2】:

Apple 提供的视图控制器这样做的方式是在呈现的视图控制器上有一个委托,当该视图控制器请求关闭时调用该委托。然后,演示者将负责关闭控制器,因此也知道何时进行任何相关的清理(在动画之前和之后)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2011-12-23
    • 2012-10-25
    • 2014-12-15
    • 1970-01-01
    相关资源
    最近更新 更多