hoc*_*ker 5 iphone exc-bad-access objective-c uitextfield ios
我有一个UITextField,我将其添加到a 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];
}
}
Run Code Online (Sandbox Code Playgroud)
要检测对文本字段的编辑,我已UITextFieldDelegate在标头中设置并捕获以下委托调用:
@interface AccountViewController : UITableViewController <UITextFieldDelegate> {
BOOL hasSearch;
UITextField *searchField;
...
}
Run Code Online (Sandbox Code Playgroud)
在实现中,我然后处理这些委托方法:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
但是在第二个中,除非我返回YES,否则文本永远不会改变; 在第一个,返回YES似乎不影响我.但是,当我在任何一个中都返回YES时,我会感到讨厌EXC_BAD_ACCESS.
我必须在我的手册中遗漏一些东西,将这个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];
}
Run Code Online (Sandbox Code Playgroud)
不过我有点困惑.当我使用textFieldShouldReturn过滤此列表然后返回NO时,它会正确过滤并且不会崩溃.关于从这些方法中的任何一个返回YES的事情会在我过滤后导致崩溃.如果我没有过滤,返回YES是没问题的.
如果有任何其他代码我应该发布,请告诉我.
我已经解决了这个问题,但我还不能(由于保密协议)在这里发布答案。那些对付费 iOS 开发者计划感兴趣的人应该去 NDA 开发者论坛并搜索 UITextField EXC_BAD_ACCESS 你会找到答案...
| 归档时间: |
|
| 查看次数: |
1288 次 |
| 最近记录: |