2011-02-27 48 views
0

我需要以编程方式创建几个按钮和标签。大多数情况下,除文本,标签和框架外,它们都具有相同的属性(不透明,backgroundColor,textColor)。以编程方式创建元素并重新使用代码

虽然试图限制我需要键入的代码量,我想这会工作:

 
// generate generic properties for our buttons 
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
tempButton.opaque = true; 
tempButton.backgroundColor = [UIColor whiteColor]; 
tempButton.titleLabel.font = [UIFont fontWithName:@"Helvitica-Bold" size:15];; 
[tempButton setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal]; 
[tempButton addTarget:self action:@selector(handleButtonTap:) forControlEvents:UIControlEventTouchUpInside]; 

// generate specific button1 properties, assign to ivar, and display 
tempButton.frame = CGRectMake(81, 136, 159, 37); 
tempButton.tag = BUTTON_1_TAG; 
[tempButton setTitle:@"button 1 title" forState:UIControlStateNormal]; 
self.button1 = tempButton; 
[self.view addSubview:self.button1]; 

// generate specific button2 properties, assign to ivar, and display 
tempButton.frame = CGRectMake(81, 254, 159, 37); 
tempButton.tag = BUTTON_2_TAG; 
[tempButton setTitle:@"button 2 title" forState:UIControlStateNormal]; 
self.button2 = tempButton; 
[self.view addSubview:self.button2]; 

由于大部分你已经知道,只有最后一个按钮显示。我认为这是因为我真的在做的是覆盖tempButton。

有没有一种解决方案可以让我完成我在这里要做的工作,而无需为每个元素创建单独的“temp”变量?

另外,它似乎会有我的代码上面的内存泄漏问题。我的理解是,tempButton最初是自动释放的,但每次我用它来设置我的伊娃时,是不是又被保留了下来?这样做后,我需要发送tempVar一个版本,所以它会回落到1,所以当autorelease被触发时,它实际上会被释放?

感谢您的帮助!

回答

2

1.)您正在为临时变量分配一个新的UIButton。这只是一项任务。作业不保留,所以你没有内存泄漏。

2)您可以返回部分配置按钮的方法,只需要设置不同的部分:

- (UIButton *)myButton { 
    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    tempButton.opaque = true; 
    tempButton.backgroundColor = [UIColor whiteColor]; 
    tempButton.titleLabel.font = [UIFont fontWithName:@"Helvitica-Bold" size:15];; 
    [tempButton setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal]; 
    return tempButton; 
} 


.... 
IButton *tempButton = [self myButton]; 
[tempButton setTitle:@"button 1 title" forState:UIControlStateNormal]; 
tempButton.frame = CGRectMake(81, 136, 159, 37); 
[self.view addSubview:tempButton]; 
tempButton = [self myButton]; 
tempButton.frame = CGRectMake(81, 254, 159, 37); 
[tempButton setTitle:@"button 2 title" forState:UIControlStateNormal]; 
[self.view addSubview:tempButton]; 

既然你已经给你的按钮独特的标签,你并不需要分配到像self.button1,self.button2这样的iVArs,您可以使用[self.view viewForTag:]来检索正确的子视图。

但是,正如另一个人所说,如果你使用'self.button1 =',该按钮将被保留,并且你需要释放你的dealloc,并且担心调用 - [view didUnload] - - 如果你没有在那里释放,那么你将有指向不再在视图中的按钮的指针。

+0

Ooh,viewForTag。我喜欢。使事情变得简单很多。你在上面的评论中提到了Cocoa命名约定。是否有一个标准的约定跟随标签,如果没有,你有你自己的? – djibouti33 2011-02-27 02:13:26

+0

我认为你的意思是viewWithTag。找不到对viewForTag的任何引用。 – djibouti33 2011-02-27 08:37:58

2

您可以创建一个构建“tempButton”的方法。

- (UIButton *)createTempButton { 
    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    tempButton.opaque = true; 
    tempButton.backgroundColor = [UIColor whiteColor]; 
    tempButton.titleLabel.font = [UIFont fontWithName:@"Helvitica-Bold" size:15];; 
    [tempButton setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal]; 
    [tempButton addTarget:self action:@selector(handleButtonTap:) forControlEvents:UIControlEventTouchUpInside]; 
    return tempButton; 
} 

然后你可以在你需要一个新的“tempButton”时调用它。

UIButton *aTempButton = [self createTempButton]; 
+0

感谢您的快速回复。为什么我们在方法的末尾自动释放它?是不是tempButton已经设置为autoreleased,因为它是用类方法实例化的? – djibouti33 2011-02-27 02:01:13

+0

哦,是的,你说得对。我会更新。 – benwong 2011-02-27 02:02:52

+0

如果它返回一个自动释放对象,则不要将其称为“createTempButton”。这是难以发现的错误的秘诀:按照可可命名约定。 – DavidPhillipOster 2011-02-27 02:06:53

0
  1. 你必须创建单独的按钮对象。由于UIButton不符合NSCopying协议,因此没有更简单的方法。如果你做的很多,写一个方法来创建一个按钮,设置公共属性并返回对象。另一种方法是制作一个Nib文件并制作实例。

  2. 是否self.button = ...保留您的按钮?是的,如果该物业的二传手保留它。当视图ID被卸载或释放时,您将不得不释放它。

0

在:

self.button1 = tempButton; 

Button1的属性可能是一个副本属性,而不是一个保留属性。然后对tempButton进行的任何更改都不会影响button1 ivar所持有的对象副本。 tempButton的保留计数也不会受到影响,因此您可以在完成初始化时释放它。

相关问题