Iphone UITextField-在返回BOOL的代理上崩溃
我有一个UITextField,我正在将其添加到Iphone UITextField-在返回BOOL的代理上崩溃,iphone,objective-c,ios,uitextfield,exc-bad-access,Iphone,Objective C,Ios,Uitextfield,Exc Bad Access,我有一个UITextField,我正在将其添加到UITableViewCell中,以用作一长串帐户的搜索字段。我补充如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [table
UITableViewCell
中,以用作一长串帐户的搜索字段。我补充如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if ((indexPath.section < accountsection) && (hasSearch) && (indexPath.row == 0))
{
// Search
if (!searchField)
{
searchField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, cell.frame.size.width - 40, 25)];
[searchField setEnabled:YES];
searchField.placeholder = NSLocalizedString(@"Search", @"search");
searchField.keyboardType = UIKeyboardTypeDefault;
searchField.returnKeyType = UIReturnKeySearch;
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
searchField.clearButtonMode = UITextFieldViewModeAlways;
searchField.delegate = self;
[cell addSubview:searchField];
[searchField release];
}
// Clean up an account label if needed
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = @"";
cell.detailTextLabel.text = @"";
// Show the search field if it was hidden by a text label
searchField.hidden = NO;
[cell bringSubviewToFront:searchField];
}
}
在实现中,我将处理这些委托方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"Done editing");
[self filterAccountsBy:textField.text];
[textField resignFirstResponder];
return NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(@"Searching for %@", string);
[self filterAccountsBy:string];
return YES;
}
但是在第二个例子中,除非我返回YES,否则文本永远不会改变;在第一个例子中,返回YES似乎对我没有影响。但是当我在其中一个中返回YES时,我会得到一个讨厌的EXC\u BAD\u访问
在我的手机中添加此UITextField时,我的手册中肯定遗漏了一些内容,但我无法确定它是什么。。。有人能帮忙吗
非常感谢
编辑:如下所示,我注释掉了filterAccounts调用,我的应用程序现在不再崩溃。以下是此方法的完整代码:
- (void)filterAccountsBy:(NSString *)filterstring
{
[accounts removeAllObjects];
if (([filterstring length] == 0) && (!isChooser) && (![vpmsConn isDomainLogon])) {
[accounts addObject:[[vpmsConn accounts] objectAtIndex:0]];
}
if ([filterstring length] == 0) {
[accounts addObjectsFromArray:[cache accounts]];
} else {
for (AccountItem *ac in [cache accounts])
{
BOOL found = NO;
// Name search
if ([[ac.clientName uppercaseString] rangeOfString:[filterstring uppercaseString]].location != NSNotFound) {
found = YES;
}
//more similar searches
if (found) {
[accounts addObject:ac];
}
}
}
[self.tableView reloadData];
}
不过,我有点困惑。当我使用textFieldShouldReturn然后返回NO筛选此列表时,它会正确筛选并且不会崩溃。从这些方法中的任何一个返回YES都会在我过滤后导致崩溃。如果我根本没有过滤,返回YES就没有问题
如果还有其他代码需要发布,请告诉我。我认为您的
UITextField
搜索字段已开始释放。如果您使用self.searchField设置您的搜索字段
(假设它是一个保留属性),这将解决此问题
我不确定你的.h的其余部分是什么样子。你可以做:
@property (nonatomic, retain) UITextField *searchField;
然后在.m中:
@synthesize searchField;
这就是我所说的将搜索字段作为保留属性的意思。这样,你的班级将保留搜索字段。我已经解决了这个问题,但是我还不能(由于保密协议)在这里发布答案。那些对付费iOS开发者计划感兴趣的人,应该去NDA开发者论坛,搜索UITextField EXC\u BAD\u访问,你会在中找到答案…
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
在哪里
return cell;
?在-(UITableView单元格*)tableView:(UITableView*)tableView单元格for行索引路径:(nsindepath*)indepath返回单元格代码>
不得在事件中释放serachfield
解除锁定释放
添加@property(非原子,保留)UITextField*searchField 什么是过滤器帐户通过:
?如果你把它注释掉,它会崩溃吗?这个方法只是通过提供的文本过滤帐户;它基本上对NSMutableArray执行以下操作:删除所有对象,然后重新添加新对象,然后在使用该数组作为其数据源的tableview上调用reloadData。但是,正如您所猜测的,当启用该方法时,它实际上会崩溃,而当注释掉该方法时不会崩溃。这让我现在想知道这种方法有什么错。我将在上面发布完整的方法代码。您可以发布崩溃日志或回溯
?如果这听起来像是一个愚蠢的问题,很抱歉,但如何获取回溯/崩溃日志?我尝试了此方法,但没有效果。从上一篇评论员的编辑中,我的过滤器逻辑出现了一些问题。我将编辑原始问题以添加此代码…如果你打开僵尸,你应该能够找到发布后引用的对象。这将有助于缩小问题的范围()事实上,我已经完全按照链接所描述的那样启用了僵尸-Xcode 4只是在main()函数中无用地转储了我。我将在上面发布回溯。。。
return cell;