ios 关闭modalviewcontroller后调用viewwillappear
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18067397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling viewwillappear after dismissing modalviewcontroller
提问by Ouassim Mouyarden
How can I call viewwillappear
after dismissing modalviewcontroller
?
viewwillappear
解雇后如何打电话modalviewcontroller
?
Any idea please because after dismissing my viewwillappear
didn't get called :
请有任何想法,因为在解雇我viewwillappear
后没有接到电话:
presenting my viewcontroller modally : //firsviewcontroller :
以模态方式呈现我的视图控制器://firsviewcontroller:
-(IBAction)AddActivity:(id)sender{
CreateActivity *addViewController = [[CreateActivity alloc] initWithNibName:@"CreateActivity" bundle:nil];
addViewController.delegate = self;
addViewController.modalPresentationStyle = UIModalPresentationFormSheet;
addViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:addViewController animated:YES];
addViewController.view.superview.frame = CGRectMake(50, 260, 680, 624);
}
//secondvioewcontroller : I create An alertview to dismiss this modalview , but the viewwillapear didn't get called:
//secondvioewcontroller :我创建了一个警报视图来关闭这个 modalview ,但是没有调用 viewwillapear :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
if ([self respondsToSelector:@selector(presentingViewController)]){
[self.presentingViewController dismissModalViewControllerAnimated:YES];
}
else {
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
}
}
回答by rdelmar
Since you're presenting the modal view controlleras a form sheet, the presenting controller's view never disappears, so viewWillAppear:
is not called after the dismissal. If you want the presenting view controller to handle something after the dismissal, call a delegate method in the modal controller's viewDidDisappear:
method. You've already set the delegate, so I presume you already have a delegate protocol in CreateActivity
.
由于您将模态视图控制器呈现为表单,因此呈现控制器的视图永远不会消失,因此viewWillAppear:
在解雇后不会被调用。如果您希望呈现视图控制器在解除后处理某些事情,请在模态控制器的viewDidDisappear:
方法中调用委托方法。您已经设置了委托,所以我认为您已经在CreateActivity
.
By the way, you should use the non-deprecated methods to present and dismiss your modal view controller.
顺便说一句,您应该使用未弃用的方法来呈现和关闭您的模态视图控制器。
回答by TomSwift
presentModalViewController:animated:
/ dismissModalViewControllerAnimated:
are deprecated. Use presentViewController:animated:completion:
/ dismissViewControllerAnimated:completion:
instead.
presentModalViewController:animated:
/dismissModalViewControllerAnimated:
已弃用。使用presentViewController:animated:completion:
/dismissViewControllerAnimated:completion:
代替。
You can use the completion block to execute any code post dismisal:
您可以使用完成块来执行任何代码 post dismissal:
- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex: (NSInteger) buttonIndex
{
if (buttonIndex == 0)
{
MyCustomViewController* mcvc = (MyCustomViewController*)self.presentingViewController;
[self dismissViewControllerAnimated: YES completion: ^{
// call your completion method:
[mcvc someCustomDoneMethod];
}];
}
}
Better yet, if you're using a storyboard then you can implement an unwind segue and trigger your completion code in the unwind callback method.
更好的是,如果您使用的是故事板,那么您可以实现一个展开转场并在展开回调方法中触发您的完成代码。