如何让应用程序运行良好

时间:2011-06-01 14:01:00

标签: iphone uiview uiviewcontroller

我有一些UIViews,第一个UIView上有一些按钮,他们的点击功能喜欢这个

-(IBAction)securityClicked:(id)sender
{ 

SwitchViewController* switchViewController = (SwitchViewController*)[wangfan_chevyAppDelegate App].viewController;
[switchViewController.wtccviewcontroller.scrollView1 setContentOffset:CGPointMake(
                            1024*0, 0) animated:false];
[[testAppDelegate App] pushViewFromRight:switchViewController.wtccviewcontroller
                                            over:self];

}

和wtccviewcontroller.m文件的代码是

- (void)viewDidLoad {
[super viewDidLoad];    

UIViewController* controller = nil;

controller = [[Wtcc1ViewController alloc]initWithNibName:@"Wtcc1ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*0, 0, 1024, 768);
[scrollView1 addSubview:controller.view];   
//[controller release];

controller = [[Wtcc2ViewController alloc]initWithNibName:@"Wtcc2ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*1, 0, 1024, 768);
[scrollView1 addSubview:controller.view];   
//[controller release];

controller = [[Wtcc3ViewController alloc]initWithNibName:@"Wtcc3ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*2, 0, 1024, 768);
[scrollView1 addSubview:controller.view];   
//[controller release];

controller = [[Wtcc4ViewController alloc]initWithNibName:@"Wtcc4ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*3, 0, 1024, 768);
[scrollView1 addSubview:controller.view];   
//[controller release];

controller = [[SecurityViewController alloc]initWithNibName:@"SecurityViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*4, 0, 1024, 768);
[scrollView1 addSubview:controller.view];   
//[controller release];

controller = [[Security2ViewController alloc]initWithNibName:@"Security2ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*5, 0, 1024, 768);
[scrollView1 addSubview:controller.view];   
//[controller release];

controller = [[Onstar1ViewController alloc]initWithNibName:@"Onstar1ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*6, 0, 1024, 768);
[scrollView1 addSubview:controller.view];   
//[controller release];

controller = [[Movie1ViewController alloc]initWithNibName:@"Movie1ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*7, 0, 1024, 768);
[scrollView1 addSubview:controller.view];   
//[controller release];

controller = [[CarownerViewController alloc]initWithNibName:@"CarownerView" bundle:nil];
controller.view.frame = CGRectMake(1024*8, 0, 1024, 768);
[scrollView1 addSubview:controller.view];   
//[controller release];

controller = [[Co01ViewController alloc]initWithNibName:@"Co01ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*9, 0, 1024, 768);
[scrollView1 addSubview:controller.view];

controller = [[Co02ViewController alloc]initWithNibName:@"Co02ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*10, 0, 1024, 768);
[scrollView1 addSubview:controller.view];

controller = [[Co03ViewController alloc]initWithNibName:@"Co03ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*11, 0, 1024, 768);
[scrollView1 addSubview:controller.view];

controller = [[Co04ViewController alloc]initWithNibName:@"Co04ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*12, 0, 1024, 768);
[scrollView1 addSubview:controller.view];

controller = [[Co05ViewController alloc]initWithNibName:@"Co05ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*13, 0, 1024, 768);
[scrollView1 addSubview:controller.view];

controller = [[Co06ViewController alloc]initWithNibName:@"Co06ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*14, 0, 1024, 768);
[scrollView1 addSubview:controller.view];

controller = [[Co07ViewController alloc]initWithNibName:@"Co07ViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*15, 0, 1024, 768);
[scrollView1 addSubview:controller.view];

controller = [[ConfigViewController alloc]initWithNibName:@"ConfigViewController" bundle:nil];
controller.view.frame = CGRectMake(1024*16, 0, 1024, 768);
[scrollView1 addSubview:controller.view];

    [scrollView1 setContentSize:CGSizeMake(1024*17,768)];
   }

因为有很多控制器,所以当我点击第一个视图上的按钮时,它会被加载很长时间,甚至退出应用程序,所以我该怎么办才能让它运行良好?

2 个答案:

答案 0 :(得分:2)

您的代码至少有三个主要问题:

  1. 你正在疯狂地泄漏物品。您正在分配多个视图控制器但从不释放它们。实际上,由于您正在重新分配controller变量,因此您将丢失对这些对象的引用,以便您永远不会释放它们。
  2. 不要使用多个UIViewController对象来控制单个视图的各个部分。视图控制器的设计旨在全屏使用(或作为UINavigationControllerUITabBarController等的一部分)。您应该使用单个UIView对象并从中央控制器对象和/或使用您自己的轻量级视图控制器控制它们。
  3. 您的最终视图大小类似于8192x768;这是巨大的。至少,您可以使用UITableViewController智能地缓存视图的各个部分。不过,这个屏幕应该可以拆分成较小的视图。

答案 1 :(得分:1)

首先,如果你调用[alloc]你创建一个对象(对于每个控制器),那么你必须释放它。您可以在添加子视图后执行此操作,因为这样可以保留它。

我看到你有时候评论了[控制器发布]然后完全忘了它。

此外,您还在这里添加了很多控制器。您可以在viewDidLoad中添加第一个,并在单击时仅延迟加载其他控制器。