【问题标题】:Calling viewwillappear after dismissing modalviewcontroller关闭modalviewcontroller后调用viewwillappear
【发布时间】:2013-08-05 20:45:16
【问题描述】:

关闭modalviewcontroller后如何拨打viewwillappear

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

以模态方式展示我的视图控制器: //第一视图控制器:

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

【问题讨论】:

  • 你确定吗?你怎么检查?根据我的经验,viewWillAppear 总是在呈现的模式被关闭时为呈现的视图控制器触发。
  • 我正在编辑我的帖子,看看我做了什么,我错过了什么?
  • @OuassimMouyarden 您需要先学习如何正确地考虑 OOP,然后再开始做事。话虽如此,我仍然会帮助你,暂时。当 UIAlertView 上的按钮被按下时,您希望发生什么?
  • 我想关闭我的 modalview ,并调用我的 firstviewcontroller 的 viewwillappear 来做一些操作
  • 代码对我有用,并且调用了 viewDidAppear。模态视图控制器被解散了吗?

标签: iphone ios objective-c


【解决方案1】:

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

您可以使用完成块来执行任何代码 post 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];
        }];
    }
}

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

【讨论】:

  • 谢谢回复,所以 [self.presentingViewController someCustomDoneMethod]; someCustomDoneMethod 必须在我的 firstviewcontroller 中定义,它会在关闭 modalview 后调用??
  • 是的。您可能必须将presentingViewController 转换为您的自定义控制器类型,以便您可以调用该方法。注意我正在对示例进行一次编辑。您不必在呈现视图控制器上调用dismissViewControllerAnimated。你可以称之为反对自我。
【解决方案2】:

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

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

【讨论】:

  • 您可以在完成中执行代码,而不是调用委托方法:对于dismissViewController:animated:completion:。如果控制器是导航堆栈的一部分(例如,您推送另一个控制器..),将代码放在 viewDidDisappear 中可能会出现问题。如果您仍想使用 viewDidDisappear,请务必检查 self.isBeingDismissed。
猜你喜欢
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多