以这种方式在UIViewController之间切换是否正确?

时间:2012-07-23 12:09:23

标签: ios uiviewcontroller

我想使用自己的控制器在uiviewcontrollers之间导航。所以我可以在我的观点之间进行自定义转换。

我做了一些有效但我不确定它是否正确的方法?以后会出现与内存有关的问题吗?......我不确定所有内容都是以正确的方式解除分配的。

感谢您的帮助!

这是我的代码:

在我的AppDelegate中,当它开始时:

self.rootVC = [[[MenuView alloc] initWithNibName:@"MenuView" bundle:nil] autorelease];
[self.window addSubview:self.rootVC.view];

在我的AppDelegate中,用于在UIViewController之间切换的方法:

-(void) changeRootController: (NSString*) ctrlName{

    UIViewController *oldVC = self.curVC;
    UIViewController *newVC;

    if(ctrlName == @"Studio"){
        newVC = [[StudioView alloc] initWithNibName:@"StudioView" bundle:nil];
    }
    else if(ctrlName == @"Menu"){
        newVC = [[MenuView alloc] initWithNibName:@"MenuView" bundle:nil];
    }

    self.curVC = newVC;

    [UIView transitionWithView:self.window
            duration:1.0
            options:UIViewAnimationOptionCurveEaseIn
            animations:^{
                newVC.view.transform = CGAffineTransformConcat(newVC.view.transform, CGAffineTransformMakeTranslation(-1024, 0));
                [self.rootVC.view addSubview:newVC.view];
                newVC.view.transform = CGAffineTransformConcat(newVC.view.transform, CGAffineTransformMakeTranslation(1024, 0));
                oldVC.view.transform = CGAffineTransformConcat(oldVC.view.transform, CGAffineTransformMakeTranslation(1024, 0));
            } 
            completion:^(BOOL finished){
                if(finished){
                    [oldVC release];
                }
            }
     ];
}

我在UIViewController中切换视图如下:

AppDelegate *ad = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[ad changeRootController:@"Studio"];

1 个答案:

答案 0 :(得分:1)

newVC未发布。

在transitionWithView之后,块应该保留newVc 你应该打电话。 (最后一行功能)

[newVC autorelease];

假设self.curVC属性强或保留