Iphone NSString错误
我创建了一个uitext视图,如下所示:Iphone NSString错误,iphone,ios,ios4,Iphone,Ios,Ios4,我创建了一个uitext视图,如下所示: in the h.file @property (nonatomic, retain) NSString *strDescription; in the m file @synthesize strDescription; - (void)viewDidLoad { GRect frame = CGRectMake(0, 0, 320, 369); tblAddEquipment = [[UITableView alloc] init
in the h.file
@property (nonatomic, retain) NSString *strDescription;
in the m file
@synthesize strDescription;
- (void)viewDidLoad
{
GRect frame = CGRectMake(0, 0, 320, 369);
tblAddEquipment = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
tblAddEquipment.delegate = self;
tblAddEquipment.dataSource = self;
//avoid reusable
[self.view addSubview:tblAddEquipment];
self.tableView.scrollEnabled = YES;
//self.tblAddEquipment.scrollEnabled = NO;
[tblAddEquipment release];
[self.tableView setSeparatorColor:[UIColor clearColor]];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
strDescription = textView.text;
NSLog(@"strDescription textView#####################--> %@", strDescription);
[tblAddEquipment reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"str Description in tableview--> %@", strDescription);
UITextView *txtDescription;
cellRectangle = CGRectMake( 175, 1, 120, 40 );
txtDescription = [[UITextView alloc] initWithFrame: cellRectangle];
txtDescription.font = font;
//txtDescription.scrollEnabled = YES;
txtDescription.textColor = [UIColor blackColor];
txtDescription.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:txtDescription];
txtDescription.returnKeyType = UIReturnKeyDone;
txtDescription.delegate = self;
txtDescription.tag = 10;
NSString *strDesc = strDescription;
NSLog(@"strDesc in tableview--> %@", strDesc);
txtDescription.text = strDesc;
[txtDescription release];
}
当我在textview中完成文本输入时,将调用textViewDiEndediting
方法
这样,我就得到了textView
文本输入。我将其存储在strDescription
变量中。我可以在那里打印出来。它正确显示分配给它的文本
但是当调用cellforrowatinexpath
方法时,我试图打印strDescription
变量,但它崩溃并显示exc\u bad\u access
我已经面对这个问题很久了。我不知道我做错了什么。请帮帮我。您保留了
strDescription
,但在您的代码中,我觉得您没有使用它,
因为下面的语句不会增加retain计数
strDescription = textView.text;
用下面的代替
self.strDescription = textView.text;
使用下面的代码
- (void)textViewDidEndEditing:(UITextView *)textView
{
self.strDescription = textView.text;
NSLog(@"strDescription textView#####################--> %@", strDescription);
[tblAddEquipment reloadData];
}
首先,这种方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
需要返回UITableViewCell对象,我想…尝试将
textViewDidFinishEditing
中的第一行更改为:
strDescription = [NSString stringWithFormatString:@"%@", textView.text];
我认为您应该使用“self.strDescription”是“cellforrowatinedexpath”在第一行(NSLog)崩溃,还是strDescription编写成功后方法崩溃?不,在cellforrowatinedexpath方法中,我无法获取strDescription(NSLog)你能给我解释一下他们之间的区别和原因吗crashing@barbgal:正如我所提到的,对该变量使用“self”invoke setter函数以及默认情况下对所有合成变量使用setter函数都有代码来增加保留计数。你的应用程序崩溃了,因为你试图删除一个过度释放的对象。@Jhaliya:不,我仔细阅读了这个问题,我只是不知道问题出在哪里。我知道合成属性在内部有一个
保留
,但我没有意识到只有在属性前面有self
时才会发生这种情况。非常感谢您提供的信息。