If I create a property of type NSString without allocating space to it and assign some value to it , it works. Where as I do the same thing for a UITextField it doesn’t..? I am creating a textfield programmatically…. any help is appreciated.
EDIT:
I have created two properties in .h of SecondViewController…
@property (nonatomic,retain) NSString *text;
@property (nonatomic, retain) UITextField *textField;
Now if I create a new object secondViewController and say do this…
secondViewController.text=@"Second View Controller";
this value is retained even though I did not alloc Memory to it…
and if i try to do the same thing for a textfield object this does not happen unless I allocate memory to it..
here is my SecondViewController…
//
// SecondViewController.m
// Tester
//
// Created by Ankit on 4/16/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "SecondViewController.h"
@implementation SecondViewController
@synthesize text,textField;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"The text is %@",text);
// textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)]; (if I `uncomment this than only this textField is visible)`
textField.frame=CGRectMake(10, 200, 300, 40);
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:textField];
// textfield=[[UITextField alloc] init];
// textfield.frame=CGRectMake(10,10,60, 30);
// [self.view addSubview:textfield];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.text=nil;
self.textField=nil;
NSLog(@"viewDidUnLoad");
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void)dealloc{
NSLog(@"dealloc");
[text release];
[textField release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
A property is just a reference to an object and the ‘space’ has to come from somewhere else. The only real difference with NSString is that the space is often in the form of a literal so you don’t see the allocation directly. @”Hello” is a real object that you can reference but in other cases you use [[NSString alloc] init…] constructions that are the same as [[UITextField alloc] init…] constructions.