26

viewwillappear解散后怎么打电话modalviewcontroller

请有任何想法,因为在解雇我viewwillappear之后没有被调用:

以模态方式展示我的视图控制器://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 :我创建了一个 alertview 来关闭这个 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];
        }
    }
}
4

2 回答 2

13

presentModalViewController:animated:/dismissModalViewControllerAnimated:已弃用。使用presentViewController:animated:completion:/dismissViewControllerAnimated:completion:代替。

您可以使用完成块执行解雇后的任何代码:

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

更好的是,如果您使用的是故事板,那么您可以实现展开转场并在展开回调方法中触发完成代码。

于 2013-08-05T22:35:12.997 回答
12

由于您将模态视图控制器呈现为表单,因此呈现控制器的视图永远不会消失,因此viewWillAppear:在解雇后不会被调用。如果您希望呈现视图控制器在解雇后处理某些事情,请在模态控制器的方法中调用委托viewDidDisappear:方法。您已经设置了委托,所以我假设您已经在CreateActivity.

顺便说一句,您应该使用不推荐使用的方法来呈现和关闭您的模态视图控制器。

于 2013-08-05T22:21:05.973 回答