0

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

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)

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). 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. Any idea why?

I'm just doing the NSLog only, because i'm trying to figure out why it isnt working

2
  • 1
    add code of your AddAction: method Commented Oct 9, 2012 at 5:04
  • paste the content of dictionary that you read from main bundle. Commented Oct 9, 2012 at 5:13

2 Answers 2

1

change

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

return YES;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh yeah, i have set that :o) I tried both in dispair, and accidently copy pasted the one that says no :-)
0

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. commenting it out solved everything, even though i don't use the dictionary after the for loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.