开发者

Getting a List of 5 recent items filled in UITextField

开发者 https://www.devze.com 2023-04-10 12:18 出处:网络
Getting a List of 5 recent items filled in Text Field, from where if i select any item will get populated in the Text Field. Open 开发者_如何学Cfor any suggestions/solution.

Getting a List of 5 recent items filled in Text Field, from where if i select any item will get populated in the Text Field. Open 开发者_如何学Cfor any suggestions/solution. Thanks in Advance.


I agree with Paul Peelen but his solution is incomplete. You want to have five different items, so the code is the following:



#define CAPACITY 5

[...]

self.recents = [NSMutableArray arrayWithCapacity:CAPACITY + 1];

[...]

- (void)addItem:(NSString*)addItem {

    //item won't be twice in the list
    [self.recents removeObject:item];

    //recently used items are at the beginning of the list
    [self.recents insertObject:item atIndex:0];

    //remove the sixth item
    if ([self.recents count] == CAPACITY + 1) {
        [self.recents removeLastObject];
    }
}



Create a NSMutableArray and listen to the UITextField's delegate. Whenever the user presses "Return" or the field is resigned, add the current value to the NSMutableArray ([myArray addObject:textField.text];)

When you want to show the data in the array:

for (NSString *value in myArray)
{
    NSLog(@"String value: %@", value);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号