6

我发现了几个关于此的问题,但答案并没有解决我的问题。

我有两个使用 presentModalViewController 呈现的控制器。

我将 modalTransitionStyle 添加到主控制器调用的第一个控制器。第一个控制器正常呈现第二个控制器(没有过渡样式)。

FirstVC *first = [[FirstVC alloc] initWithNibName:@"FirstVC" bundle:nil];
first.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:first animated:YES];

SecondVC *second = [[SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
[self presentModalViewController:second animated:YES];

这是我用来访问 MainVC 的代码:

[self.presentingViewController.presentingViewController dismissModalViewControllerAnimated:YES];

这就是发生的事情:

在此处输入图像描述

页面没有展开。我遇到这种情况的原因是什么?

4

6 回答 6

6

在我的实验中,您似乎无法在部分卷曲后进行标准演示(垂直封面),并同时关闭它们,除非您在动画设置为 NO 的情况下进行关闭。

解决此问题的一种方法是在没有动画的情况下关闭 secondVC(此代码在 secondVC 中):

-(IBAction)dismissSelf:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

然后在第一个VC中再次在viewDidAppear中用动画关闭,在测试控制器没有被呈现后:

-(void)viewDidAppear:(BOOL)animated {
    if (![self isBeingPresented]) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

虽然上面的代码可以返回到初始控制器的视图,但您会看到 firstVC 的视图出现在 curl 展开之前。如果您不想看到它,那么我能找到解决该问题的唯一方法是创建 secondVC 的图像,在解除 secondVC 之前将其作为(在图像视图中)子视图添加到 firstVC。因此,要做到这一点,secondVC 中的代码应该是这样的(请注意,您必须链接到 QuartzCore 并将其导入 secondVC 才能正常工作):

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIImage *img = [self imageWithView:self.view];
    FirstViewController *first = (FirstViewController *)self.presentingViewController;
    UIImageView *iv = [[UIImageView alloc] initWithFrame:first.view.bounds];
    iv.image = img;
    [first.view addSubview:iv];
}


-(IBAction)dismissSelf:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}


- (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
于 2013-08-29T03:50:31.047 回答
3

您应该通过以下调用一个接一个地呈现您的两个视图:

[self presentViewController:firstViewController animated:YES completion:^(
    [self presentViewController:secondViewController animated:YES completion:^(

    }];
}];

这应该表现得更好。另外,要知道之后您必须将这两个 viewController 都拉下来。

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:^{

    }];
}];
于 2013-08-29T03:28:56.147 回答
2

It looks like you are dismissing 'first' without dismissing 'second.'

The Apple recommended way of doing this is with delegation.

Try making the mainVC a delegate of 'first' so that first can call a method of mainVC to dismiss itself. Do this by declaring a 'delegate' property of 'first' and setting it to mainVC when 'first' is instantiated. then define a 'firstDelegate' protocol in first.h which includes some function like 'dismissFirst' and then import first.h in mainVC. now implement dismissFirst in mainVC to actually dismiss 'first' and do whatever else you want now that first is done being displayed.

gasp... now make 'first' a delegate of 'second' in the same way, and just make the 'dismissSecond' function call the 'dismissFirst' function of mainVC and all will be right with the world.

I know this is kindo complicated but delegation is a core iOS concept and this is the quintessential example of where it is used.

Here is a good explanation of how this works. http://chrisrisner.com/31-Days-of-iOS--Day-6%E2%80%93The-Delegate-Pattern

And I wish you the best of luck in your never ending quest to understand the inner machinations of an Apple engineer's mind also known as Obj-C.

于 2013-08-29T03:47:46.000 回答
0

我也有同样的疑问。我用其他方式解决了-

第 1 步:在全局类(单例)中,您可以定义一个变量标志。

@property (nonatomic) int flag;

第 2 步:假设您需要关闭second view controllerfirst view controller返回到Main view controller,在关闭第二个视图控制器时,您可以将此值设置为假设 1

[[Global sharedObject] setFlag:1];
[self.navigationController popViewControllerAnimated:YES];

第 3 步:在您- (void)viewWillAppear:(BOOL)animated的方法中,first view controller您可以设置此方法 -

if([[Global sharedObject] flag] == 1)
{
    [[Global sharedObject] setFlag:0];
    [self performSelector:@selector(back) withObject:nil afterDelay:0.5];
}

- (void) back
{
[self.navigationController popViewControllerAnimated:YES];
}

我希望你的答案可以用这个来解决。如果你还有疑问,你可以问。干杯:)

于 2013-08-29T04:59:35.067 回答
0
- (UIViewController*)topViewController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController;
}


- (void)dismissAllModalController{

    __block UIViewController *topController = [self topViewController];

    while (topController.presentingViewController) {
        [topController dismissViewControllerAnimated:NO completion:^{

        }];
        topController = [self topViewController];
    }
}
于 2014-06-17T16:34:14.223 回答
0

您需要使用unwind segue. 在您想要连接到的视图控制器中创建一个“空”IBAction。

@IBAction func unwindToController(segue: UIStoryboardSegue) {

}

然后转到您的故事板并选择您想要来自的控制器。在所选控制器顶部的图标中,最左边应该是一个黄色圆圈,中间有一个白色小方块,也就是文件所有者。按住 Ctrl 并拖动到要展开的视图控制器的“退出”标签。出口可以在控制器树的底部附近找到,或者在情节提要上作为最右边的红色正方形图像,其中带有带有右箭头的白色正方形。这将弹出一个对话框,选择unwindToController您创建的操作。

一个新的“Unwind segue to Exit”将出现在您的 FROM 控制器中。双击它并给它一个标识符,例如unwindIdentifier

现在,当您想在 FROM 控制器中展开两个控制器时,请使用:

self.performSegueWithIdentifier("unwindIdentifier", sender:nil)

这将跳过中间控制器并展开到第二个控制器,仅在动画中显示 TO 控制器。

于 2016-05-09T16:52:16.393 回答