ios - 关闭模态视图时不调用 ViewDidAppear

标签 ios objective-c iphone viewcontroller modalviewcontroller

首先,我创建了一个 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 何时被关闭。

最佳答案

当您关闭模态视图 Controller 时,您可以使用委托(delegate)在 MainViewController 中调用您的函数。例如:

主视图 Controller .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];
}

关于ios - 关闭模态视图时不调用 ViewDidAppear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29271880/

相关文章:

ios - UIView alpha 与 UIColor alpha

ios - iOS 7 在后台接收电池状态变化通知

android - 应用程序可以读取 DNS TXT 记录吗?

xcode - 我还需要 Entitlements.plist 文件来进行临时构建吗?

ios - 调用 [self.view addSubview :self.overlayView] 后 ScrollView 重置其位置的问题;

ios - 如何将 JSON 数据放入 Objective-C 中的另一个 JSON 数据?

iphone - 如何使 UINavigationBar 不下推 View ?

iphone - 创建一个调度方法以在 Objective C (Xcode) 中以 7 天轮换显示自定义标签

iphone - 是否可以在没有导航 Controller 的情况下推送 View Controller ?

ios - 更换被拒绝的 iPhone 应用程序,是否会触发全新的审核流程?