Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 具有根模板视图的双视图应用程序_Iphone_Xcode_Ios4_Ios Simulator - Fatal编程技术网

Iphone 具有根模板视图的双视图应用程序

Iphone 具有根模板视图的双视图应用程序,iphone,xcode,ios4,ios-simulator,Iphone,Xcode,Ios4,Ios Simulator,嗨,我想做的应用程序有一个根模板视图,上面有一个漂亮的标志,或者在下面加载其他视图 (以下是我现在拥有的: ) 但是我有一个小问题。如果我删除第58行TestAppDelegate.m文件中的注释 //[currentView release]; 当我尝试切换视图时,应用程序将因大量错误而崩溃。 但如果我发表评论,这个项目分析器会告诉我,我有一个潜在的漏洞 使用currentView变量 有人能抽出一些时间看看我在代码中做错了什么吗?我有一个函数,它看起来像这样: - (void) s

嗨,我想做的应用程序有一个根模板视图,上面有一个漂亮的标志,或者在下面加载其他视图

(以下是我现在拥有的: )

但是我有一个小问题。如果我删除第58行TestAppDelegate.m文件中的注释

    //[currentView release];
当我尝试切换视图时,应用程序将因大量错误而崩溃。 但如果我发表评论,这个项目分析器会告诉我,我有一个潜在的漏洞 使用currentView变量


有人能抽出一些时间看看我在代码中做错了什么吗?

我有一个函数,它看起来像这样:

- (void) switchView: (int) viewType {

for (UIView *view in [self.viewController.rootView subviews]) {
    [view removeFromSuperview];
}

UIViewController *currentView = nil;

switch (viewType) {
    case 1:
        currentView = [[First alloc] initWithNibName:@"First" bundle:nil];
        break;

    case 2:
        currentView = [[Second alloc] initWithNibName:@"Second" bundle:nil];
        break;

}

[self.viewController.rootView addSubview: [currentView view]];
[self.window makeKeyAndVisible];

//[currentView release];

}问题在于,您只添加了
currentView.view
作为子视图,以便将其保留在其他位置,而
currentView
本身则不是。这意味着,当您释放它时,它将被解除锁定,没有它,它的视图将很难工作

一种解决方案是将
currentView
作为实例变量,并为其创建一个属性,以便为您完成内存管理

@property (nonatomic, retain) UIViewController *currentView;
然后替换一条线,如

currentView = [[First alloc] initWithNibName:@"First" bundle:nil];


这将在保留新的视图控制器之前释放旧的视图控制器。最后,别忘了在类的
dealoc
方法中释放
currentView

您是如何创建currentView的?你在分配吗?不要把这个作为答案。您可以使用问题底部的链接编辑问题。。
self.currentView = [[First alloc] initWithNibName:@"First" bundle:nil];