如果我创建 NSString 类型的属性而不为其分配空间并为其分配一些值,则它可以工作。当我为 UITextField 做同样的事情时,它不会......?我正在以编程方式创建一个文本字段....任何帮助表示赞赏。
编辑:
我在 SecondViewController 的 .h 中创建了两个属性...
@property (nonatomic,retain) NSString *text;
@property (nonatomic, retain) UITextField *textField;
现在,如果我创建一个新对象 secondViewController 并说这样做...
secondViewController.text=@"Second View Controller";
即使我没有为它分配内存,这个值也会被保留......
如果我尝试对文本字段对象做同样的事情,除非我为它分配内存,否则不会发生这种情况。
这是我的 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