sir i am new in iphone i am creating UITextField using this code but when i am releasing this in dealloc application crashes. i want to make textfield by coding. thanks in advance.
#import "TextField.h"
@implementation TextField
UILabel *label;
UITextField *textField;
- (void)viewDidLoad
{
[super viewDidLoad];
//Create label
label = [[UILabel alloc] init];
label.frame = CGRectMake(10, 10, 300, 40);
label.textAlignment = UITextAlignmentCenter;
label.text = @"";
[self.view addSubview:label];
[label release];
// Initialization code
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 50)];
textField.delegate = self;
textField.placeholder = @"<Enter Text>";
textField.textAlignment = UITextAlignmentCenter;
[self.view addSubview: textField];
[textField release];
}
- (void)dealloc
{
[textField release];
[label release];
[super dealloc];
}
@end
You have already released your text field and label by : [textField release]; ,[label release];
[textField release]; ,[label release];
than you should not release it again in dealloc
method.You are over releasing your text filed and label and that cause crash your application. Just remove it from dealloc
method.
you are using [textField release]
in two places:
1) in ViewDidLoad
2) in dealloc method
You don't have to do this in two placed. Remove the [textField release]
from viewDidLoad method.
This is because when you alloc an instance it retain count becomes 1
and when you are release it, the retain count becomes 0
. So again release the same instance is causing the crash.
use this code... u have release label and text field two times ...
#import "TextField.h"
@implementation TextField
UILabel *label;
UITextField *textField;
- (void)viewDidLoad {
[super viewDidLoad];
//Create label
label = [[UILabel alloc] init];
label.frame = CGRectMake(10, 10, 300, 40);
label.textAlignment = UITextAlignmentCenter;
label.text = @"";
[self.view addSubview:label];
[label release];
// Initialization code
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 50)];
textField.delegate = self;
textField.placeholder = @"<Enter Text>";
textField.textAlignment = UITextAlignmentCenter;
[self.view addSubview: textField];
[textField release];
}
@end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.