2010-08-13 39 views
0

我在文字fiels获取内存泄漏请建议我如何解决它。内存泄漏在iphone中的文本字段sdk

我写在初始化()的代码为:

  eventTextField = nil; 
    eventTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 15, 300, 50)]; 
    eventTextField.placeholder = @"Event Name"; 
    [eventTextField setFont:[UIFont boldSystemFontOfSize:14]]; 
    eventTextField.returnKeyType = UIReturnKeyDone; 
    eventTextField.keyboardAppearance = UIKeyboardAppearanceDefault;//Showing leak at this line 
    eventTextField.keyboardType = UIKeyboardTypeDefault; 
    eventTextField.delegate=self; 

和我释放它在dealloc方法。

和cellForRowIndex我将它添加为子视图。

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MasterViewIdentifier"]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MasterViewIdentifier"] autorelease]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    UIView* elementView = [[UIView alloc] initWithFrame:CGRectMake(20,170,320,280)]; 
    elementView.tag = 0; 
    elementView.backgroundColor=[UIColor clearColor]; 
    [cell.contentView addSubview:elementView]; 
    [elementView release]; 
} 
UIView* elementView = [cell.contentView viewWithTag:0]; 
elementView.backgroundColor=[UIColor clearColor]; 
for(UIView* subView in elementView.subviews) 
{ 
    [subView removeFromSuperview]; 
} 
if(indexPath.section == 0 && indexPath.row == 0) 
{ 
    CorkItAppDelegate* appDelegate = (CorkItAppDelegate*)[[UIApplication sharedApplication] delegate]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    wineNameTitleLabel.numberOfLines = 0; 
    [wineNameTitleLabel setFont:[UIFont boldSystemFontOfSize:14]]; 

    if(appDelegate.isNewWine == YES) 
    { 
     wineNameTitleLabel.textColor = [UIColor blackColor] ; 
     wineNameTitleLabel.text = appDelegate.getNewWineName; 
    } 
    else 
    { 
     if([event.eventWineName length]>0) 
     { 
      wineNameTitleLabel.textColor = [UIColor blackColor] ; 
      wineNameTitleLabel.text = event.eventWineName; 
     } 
     else 
     { 
      wineNameTitleLabel.text = @"Wine Name"; 
     } 
    } 
     [elementView addSubview:wineNameTitleLabel]; 
} 

else if(indexPath.section == 1) 
{ 

    if(isRightButton == YES) 
    { 
     eventTextField.enabled = NO; 
    } 
    else 
    { 
     eventTextField.enabled = YES; 
    } 
    if([event.eventName length] > 0) 
    { 
     eventTextField.text = event.eventName; 
    } 
    else 
    { 
     eventTextField.text = @""; 
    } 
    [elementView addSubview:eventTextField]; 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
     return cell; 
} 

希望我从你身边得到wucik回应。 在此先感谢, Monish。

回答

0

如果你仍然需要eventTextField。你应该为它创建一个属性和实例变量。所以,你可以让你的类拥有eventTextField并在dealloc中释放它。像这样:

@interface A { 
    UITextField *eventTextField; 
} 

@property (nonatomic, retain) UITextField *eventTextField; 

@end 

@implementation A 
@synthesize eventTextField; 

- (id)init { 
    eventTextField = [[UITextField alloc]init]; 
} 

- (void)dealloc { 
    [eventTextField release]; 
} 
@end 
+0

即使它显示内存泄漏,我也已经完成了你的建议。是否还有其他建议? – 2010-08-13 08:19:39

+0

对不起,我没有清楚地阅读你的问题。你在dealloc中释放它,所以这段代码不会帮助。你是否将你的eventTextField添加到任何子视图?或者将它删除? – vodkhang 2010-08-13 08:26:40

+0

我发布了代码,我做了subview.Please建议与任何其他想法。 – 2010-08-13 09:02:41

0

,你应该将它添加到子视图后释放的UITextField

[foo addSubview:eventTextField]; 
[eventTextField release]; 

eventTextField =零;是不必要的顺便说一句,因为eventTextField是一个伊娃已经归零(指针设置为0x0(零))

+0

但我得到Exception.I在keyBoardWillShow方法和textfieldDidchange和DoneAction中使用textfield。 – 2010-08-13 08:02:48

+0

“例外”? 如果您按照我的建议去除您在对象上执行的其他版本。 addSubview:保留该对象,所以如果在创建对象后将其添加为子视图,则可以安全地释放该对象,并且它仍然存在,直到从子视图中删除为止,因此您可以随时随地使用它 – valexa 2010-08-13 08:12:24

+0

Hum,for他发布的代码。他添加了SubView,但随后他在一段时间内删除了FromFromSuperView,所以当时eventTextField变得不受欢迎 – vodkhang 2010-08-13 09:19:25