Iphone 将多个子视图添加到viewcontrollers默认视图会使我的应用程序变慢吗?
我有一个应用程序,我在其中创建了许多UIView,并将它们添加到UIViewController的self.view中。我的应用程序运行得很慢。我正在释放所有对象,并且没有内存泄漏(我运行了性能工具)。有人能告诉我是什么让我的应用程序这么慢吗?(代码如下) [编辑]该阵列大约有30个项目。[/EndEdit] 非常感谢 以下是my UIViewController的loadView方法的代码:Iphone 将多个子视图添加到viewcontrollers默认视图会使我的应用程序变慢吗?,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我有一个应用程序,我在其中创建了许多UIView,并将它们添加到UIViewController的self.view中。我的应用程序运行得很慢。我正在释放所有对象,并且没有内存泄漏(我运行了性能工具)。有人能告诉我是什么让我的应用程序这么慢吗?(代码如下) [编辑]该阵列大约有30个项目。[/EndEdit] 非常感谢 以下是my UIViewController的loadView方法的代码: - (void)loadView { UIView *contentView = [[UIV
- (void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
int length = 0;
for(NSString *item in arrayTips)
{
length++;
[item release];
}
int index = 0;
for(NSString *item in arrayTitles)
{
SingleFlipView *backView = [[SingleFlipView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
backView.userInteractionEnabled = YES;
backView.backgroundColor = [UIColor whiteColor];
[backView setViewIndex:index];
[backView setLastViewIndex:length];
CGRect labelFrame = CGRectMake(10.0f, 0.0f, 300.0f, 30.0f);
UILabel *backLabel = [[UILabel alloc] initWithFrame:labelFrame];
backLabel.textAlignment = UITextAlignmentCenter;
backLabel.userInteractionEnabled = YES;
backLabel.text = item;
backLabel.font = [UIFont fontWithName:@"Georgia" size:24.0f];
backLabel.textColor = [UIColor blackColor];
backLabel.backgroundColor = [UIColor whiteColor];
CGRect textFrame = CGRectMake(10.0f, 30.0f, 300.0f, 110.0f);
UITextView *tbxView = [[UITextView alloc] initWithFrame:textFrame];
tbxView.textAlignment = UITextAlignmentCenter;
tbxView.userInteractionEnabled = YES;
tbxView.editable = FALSE;
tbxView.text = [arrayTips objectAtIndex:index];
tbxView.font = [UIFont fontWithName:@"Arial" size:14.0f];
tbxView.textColor = [UIColor blackColor];
tbxView.backgroundColor = [UIColor whiteColor];
//CGRect labelFrame = CGRectMake(10.0f, 0.0f, 84.0f, 30.0f);
UIImage *nextTip = [[UIImage imageNamed:@"NextTip.png"] retain];
UIImageView *nextTipView = [ [ UIImageView alloc ] initWithImage:nextTip];
nextTipView.frame = CGRectMake(230.0f, -10.0f, 84.0f, 30.0f);
nextTipView.userInteractionEnabled = YES;
UIImageView *view = [[ UIImageView alloc ] init];
view.userInteractionEnabled = YES;
if(self.sexString == @"Men")
{
UIImage *imgTip = [[UIImage imageNamed:@"feet_small.jpg"] retain];
view.image = imgTip;
view.frame = CGRectMake(0.0f, 110.0f, 416.0f, 228.0f); //59*161
[imgTip release];
}
[backView addSubview:view];
[backView addSubview:tbxView];
[backView addSubview:backLabel];
//[backView addSubview:nextTipView];
[self.view addSubview:backView];
[backView release];
[backLabel release];
[nextTip release];
[nextTipView release];
[tbxView release];
[view release];
index++;
[item release];
}
}
这将取决于数组中有多少项。如果你只是增加一两个,你不应该看到一个巨大的减速;更多,你会的。您可能应该看看UITableView处理其单元格的方式;只创建实际需要/使用的内容,或者,更好的是,只创建其中一个,并动态设置其内容 其他一些注意事项:
- ==在Objective-C中不是有效的字符串比较运算符;使用[string1 isEqualTo:string2]
- 看起来你试图同时在屏幕上放置很多这样的东西,这似乎没有多大意义
- 看起来你在最后有一个虚假的
(你永远不会保留[item release]
项,所以没有必要释放它)
- 整个第一个循环(
让我感到害怕和困惑;nsarray中的项已经被数组保留了。您不应该以这种方式显式地保留/释放它们for(NSString*item in arrayTips).
2) 如果您当前正在更改索引以使后视图可见,请调用新方法来实际构建和附加后视图。不要忘记尽可能多地使视图不透明。透明视图是性能问题的主要来源。谢谢您的注释,很高兴能得到一些关于我的代码的反馈。这是我第一个真正的应用程序,所以我仍在学习如何最好地构建我的应用程序。这是反复试验。我可能不需要所有这些视图,但可以根据数组简单地更改标签文本。