简体   繁体   English

Xcode使用textField shouldChangeCharactersInRange:导致应用崩溃

[英]Xcode Using textField shouldChangeCharactersInRange: crashes app

I'm trying to implement an autocomplete funktion under a textfield, using a script i found here: 我正在尝试使用在这里找到的脚本在文本字段下实现自动完成功能:

http://www.raywenderlich.com/336/how-to-auto-complete-with-custom-values http://www.raywenderlich.com/336/how-to-auto-complete-with-custom-values

But it runs the script as it should, and then the app crashes without giving me a reson (just exc_bad_access in the TUAppDelegate) 但是它按原样运行脚本,然后应用崩溃而没有引起我的共鸣(只是TUAppDelegate中的exc_bad_access)

No i have a few other keyboard methos, so don't know if they conflict. 不,我还有其他几种键盘方法,所以不知道它们是否冲突。

The textfield is declared like this: 文本字段的声明如下:

mytextfield_textfield = [[UITextField alloc] initWithFrame:CGRectMake(60, 57, 145, 20)];
mytextfield_textfield.borderStyle = UITextBorderStyleNone;
mytextfield_textfield.font =  [UIFont fontWithName:@"NeoSans" size:14];
mytextfield_textfield.placeholder = @"Enter something";
mytextfield_textfield.text = @"";
mytextfield_textfield.secureTextEntry = NO;
mytextfield_textfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
mytextfield_textfield.autocorrectionType = UITextAutocorrectionTypeNo;
mytextfield_textfield.keyboardType = UIKeyboardTypeDefault;
mytextfield_textfield.returnKeyType = UIReturnKeyDone;
mytextfield_textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
mytextfield_textfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
mytextfield_textfield.delegate = self;
[self.view addSubview:mytextfield_textfield];

And then the autocompleteview underneath as a scrollview instead of tableview (another reason for that). 然后将autocompleteview作为滚动视图而不是tableview放在下面(这是另一个原因)。 But it isn't used right now. 但是现在不使用。

Then i have these methods already: 那么我已经有了这些方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [mytextfield_textfield resignFirstResponder];
    }

    [mytextfield_textfield resignFirstResponder];
    myTableView.userInteractionEnabled = YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    myTableView.userInteractionEnabled = NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [mytextfield_textfield resignFirstResponder];
    myTableView.userInteractionEnabled = YES;
    [self performSelector:@selector(AddAction:) withObject:nil] ;
    return YES; 
}

And now i implement the autocomplete: 现在我实现了自动完成功能:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    autocompleteView.hidden = NO;
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    myTableView.userInteractionEnabled = YES;
    NSLog(@"ok") ;
    return NO;
}

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
    NSDictionary *dictionary = [[NSDictionary alloc] init ];
    NSString *path = [[NSBundle mainBundle] bundlePath] ;
    NSString *dictionary_path = [path stringByAppendingPathComponent:@"categorieslist2.plist"] ;
    dictionary = [NSDictionary dictionaryWithContentsOfFile:dictionary_path] ;

    for(id key in dictionary) {
        NSLog(@"%@",key) ;
    }
    [dictionary release] ;
}

It output's the keys and returns the OK from the NSlog, but then it crashes. 它输出密钥,并从NSlog返回OK,但是随后崩溃。 Any idea why? 知道为什么吗?

I'm just doing the NSLog only, because i'm trying to figure out why it isnt working 我只是在做NSLog,因为我试图弄清楚为什么它不起作用

change 更改

   - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

return YES;
}

For reason i have no idea of, the [dictionary release] was the culprit. 出于某种原因,我不知道,[词典发布]是罪魁祸首。 Anybody can explain why? 有人可以解释为什么吗? The same thing gave me a crash in another section of the app where i also released a dictionary read from a plist. 同样的事情使我在应用程序的另一部分崩溃,在那里我还发布了从plist中读取的字典。 commenting it out solved everything, even though i don't use the dictionary after the for loop 评论它解决了一切,即使我在for循环后不使用字典

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM