我有一些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)];
}
因为有很多控制器,所以当我点击第一个视图上的按钮时,它会被加载很长时间,甚至退出应用程序,所以我该怎么办才能让它运行良好?
答案 0 :(得分:2)
您的代码至少有三个主要问题:
controller
变量,因此您将丢失对这些对象的引用,以便您永远不会释放它们。UIViewController
对象来控制单个视图的各个部分。视图控制器的设计旨在全屏使用(或作为UINavigationController
,UITabBarController
等的一部分)。您应该使用单个UIView
对象并从中央控制器对象和/或使用您自己的轻量级视图控制器控制它们。UITableViewController
智能地缓存视图的各个部分。不过,这个屏幕应该可以拆分成较小的视图。答案 1 :(得分:1)
首先,如果你调用[alloc]你创建一个对象(对于每个控制器),那么你必须释放它。您可以在添加子视图后执行此操作,因为这样可以保留它。
我看到你有时候评论了[控制器发布]然后完全忘了它。
此外,您还在这里添加了很多控制器。您可以在viewDidLoad中添加第一个,并在单击时仅延迟加载其他控制器。