当模态视图被解除时,ViewDidAppear未被调用。

8
首先,我创建一个MainViewController。然后在MainViewController中,我执行以下操作:
[self presentViewController:modalViewController animated:YES completion:nil];
modalViewController.modalPresentationStyle = UIModalPresentationFormSheet;

当我关闭模态视图控制器时,iPhone(除了iPhone 6+)会调用MainViewController的viewDidAppear。但在iPad和iPhone 6+上,不会调用MainViewController的viewDidAppear。
逻辑是在关闭模态视图控制器时调用函数。如何知道何时关闭了模态视图控制器。

尝试使用其他的'modalPresentationStyle'。这样行吗? - itsji10dra
这是UX所要求的,我无法更改。 - Gonghan
你在调用:[super viewDidAppear:animated]; - itsji10dra
是的,我打了电话。我认为问题在于在iPhone上,模态视图占据整个屏幕,而在iPad上,模态视图仅位于中心。 - Gonghan
我尝试重现您的情况,这里是代码。对我来说,在所有设备上都正常工作。请参考它。希望能有所帮助。 - itsji10dra
2个回答

7
当你关闭模态视图控制器时,你可以使用代理调用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在Storyboard中定义而不是在代码中定义的情况。 - Vyachaslav Gerchicov

3
苹果提供的视图控制器实现这一功能的方式是,在被呈现的视图控制器上设置一个委托,当该视图控制器请求关闭时调用。然后,呈现者将负责关闭控制器,并且还将知道何时进行任何相关的清理(在动画之前和之后都要清理)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接