在解除modalviewcontroller后调用viewwillappear

时间:2013-08-05 20:45:17

标签: iphone ios objective-c

解雇viewwillappear后如何致电modalviewcontroller

有任何想法,因为在解雇我的viewwillappear之后没有被召唤:

以模态方式呈现我的viewcontroller: // 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来解除这个模态视图,但是没有调用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];
        }
    }
}

3 个答案:

答案 0 :(得分:13)

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

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

答案 1 :(得分:13)

presentModalViewController:animated: / dismissModalViewControllerAnimated:已弃用。请改用presentViewController:animated:completion: / dismissViewControllerAnimated:completion:

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

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

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

答案 2 :(得分:0)

您可以使用:

[self dismissViewControllerAnimated:YES completion:^{

}];