0

我有两个带视图的视图控制器。第一个是登录屏幕,第二个是从网络上获取内容(无关紧要)。

我使用了自定义的 segue 动画,并且不得不对 superview 做一些奇怪的事情,以使 sourceViewController.view 位于destinationViewController.view 的“顶部”(视觉上)

我只能假设这就是为什么当我尝试从第二个视图调用 IBAction 方法时,它们不会调用。

这是segue类的实现:

- (void) perform {

    UIViewController *sourceViewController = (UIViewController *) self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;

    UIView *parent = sourceViewController.view.superview;
    [sourceViewController.view removeFromSuperview];
    [parent addSubview: destinationViewController.view];
    [parent addSubview:sourceViewController.view];
    sourceViewController.view.layer.masksToBounds = NO;
    sourceViewController.view.layer.cornerRadius = 8; // if you like rounded corners
    sourceViewController.view.layer.shadowOffset = CGSizeMake(0,0);
    sourceViewController.view.layer.shadowRadius = 10;
    sourceViewController.view.layer.shadowOpacity = 1;

    destinationViewController.view.frame = CGRectMake(0, 20, destinationViewController.view.frame.size.width, destinationViewController.view.frame.size.height);
    sourceViewController.view.frame = CGRectMake(0, 20, sourceViewController.view.frame.size.width, sourceViewController.view.frame.size.height);

    [UIView animateWithDuration:.6
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{

                         sourceViewController.view.frame = CGRectMake(-sourceViewController.view.frame.size.width-10, 20, sourceViewController.view.frame.size.width, sourceViewController.view.frame.size.height);
                     }
                     completion:^(BOOL finished){
                         //[destinationViewController.view removeFromSuperview];
                         [sourceViewController.navigationController pushViewController:destinationViewController animated:NO];
                     }];
}

我的问题是,是否可以从其超级视图中删除源视图并以在第二个视图上调用 IBActions 的方式来解决这个问题?

IBAction 方法只是让应用程序崩溃,例如按下按钮。

4

1 回答 1

0

我通过将代码更改为此解决了我的问题:

UIViewController *sourceViewController = (UIViewController *) self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;
UIView *parent = sourceViewController.view.superview;
[parent addSubview:destinationViewController.view];
[parent sendSubviewToBack: destinationViewController.view];
    sourceViewController.view.layer.masksToBounds = NO;
    sourceViewController.view.layer.cornerRadius = 8; // if you like rounded corners
    sourceViewController.view.layer.shadowOffset = CGSizeMake(0,0);
    sourceViewController.view.layer.shadowRadius = 10;
    sourceViewController.view.layer.shadowOpacity = 1;
destinationViewController.view.frame = CGRectMake(0, 20, destinationViewController.view.frame.size.width, destinationViewController.view.frame.size.height);
    sourceViewController.view.frame = CGRectMake(0, 20, sourceViewController.view.frame.size.width, sourceViewController.view.frame.size.height);

    [UIView animateWithDuration:.6
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{

                         sourceViewController.view.frame = CGRectMake(-sourceViewController.view.frame.size.width-10, 20, sourceViewController.view.frame.size.width, sourceViewController.view.frame.size.height);
                     }
                     completion:^(BOOL finished){
                         [sourceViewController presentViewController:destinationViewController animated:NO completion:NULL];
                     }];

一些事情发生了变化,但值得注意的是,我使用了

[sourceViewController presentViewController:destinationViewController animated:NO completion:NULL];

正确初始化控制器。希望这对将来的其他人有所帮助。

于 2012-09-17T20:37:35.850 回答