EddyBorja / MLPAutoCompleteTextField

UITextfield subclass with autocomplete menu. For iOS.
Other
1.21k stars 223 forks source link

Adding MLPAutoCompleteTextField to a view programitacally #1

Closed patrick-fitzgerald closed 11 years ago

patrick-fitzgerald commented 11 years ago

Hi, First of all thanks for a great plugin. I'm using the example project and I've been trying to add the MLPAutoCompleteTextField to the MLPViewController programatically. I've deleted the view.xib file, but I cannot seem to get the custom text field to appear in the view programatically. Could you please advise how I can achieve this. I've included my viewDidLoad implementation below. Thanks again, Patrick

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.autocompleteTextField = [[MLPAutoCompleteTextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
    self.autocompleteTextField.delegate = self;
    self.autocompleteTextField.autoCompleteDataSource = self;
    self.autocompleteTextField.autoCompleteDelegate = self;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShowWithNotification:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideWithNotification:) name:UIKeyboardDidHideNotification object:nil];

    //Supported Styles:
    //[self.autocompleteTextField setBorderStyle:UITextBorderStyleBezel];
    //TODO[self.autocompleteTextField setBorderStyle:UITextBorderStyleLine];
    //[self.autocompleteTextField setBorderStyle:UITextBorderStyleNone];
    [self.autocompleteTextField setBorderStyle:UITextBorderStyleRoundedRect];

    //You can use custom TableViewCell classes and nibs in the autocomplete tableview if you wish.
    [self.autocompleteTextField registerAutoCompleteCellClass:[MLPCustomAutoCompleteCell class]
                                       forCellReuseIdentifier:@"CustomCellId"];

    [self.view addSubview:self.autocompleteTextField];

}
EddyBorja commented 11 years ago

Using your code I got a warning that the autocompleteTextField was declared as a weak property in MLPViewController. I was only able to get the textfield to appear by changing it to strong. If you don't do that it will deallocate before it gets the chance to be added to self.view

patrick-fitzgerald commented 11 years ago

Thanks Eddy, that was the problem. Cheers

harryhow commented 9 years ago

@paddydub Hi, I follow your code to use addSubview to create my view. The textfield appears but always having dataSource exception, I also noticed that there's a warning about assigning incompatible strong/weak to autoCompleteDataSource. Do you have any idea about this problem maybe causing dataStore expception? Thanks!

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An autocomplete datasource must implement either autoCompleteTextField:possibleCompletionsForString: or autoCompleteTextField:possibleCompletionsForString:completion
screen shot 2015-07-10 at 4 35 26 pm
hardikamal commented 8 years ago

Hi @EddyBorja @harryhow Facing the same problem...any solution found???

alenalexander commented 7 years ago

Hey , I managed to found the solution as below. It was regarding the change in the syntax of Swift 3.0

func autoCompleteTextField(_ textField: MLPAutoCompleteTextField!, possibleCompletionsForString string: String!) -> [AnyObject]! { return myList as [AnyObject]! }