Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6649949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:50:06+00:00 2026-05-26T00:50:06+00:00

My issue is that NSTokenField does not allow me to type any text I

  • 0

My issue is that NSTokenField does not allow me to type any text I want, it’s only allow me to type strings that are included in the NSArray that tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem: returns.

- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex      {
return [NSArray arrayWithObjects:@"AA", @"BB", @"CC", @"DD", nil];
}

My NSTokenField can only contain the above text-tokens.
If I type for example XXX it does not appeared and it cannot be added.

Why this happens since the documentation mentions “The user could enter a string that is not in the list of possible completions and that is also tokenized.”

What am I missing ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:50:07+00:00Added an answer on May 26, 2026 at 12:50 am

    The default value for selectedItemIndex is 0 — the first item in your return list.

    So you either need to set this to -1 in the case that substring isn’t represented in your list (otherwise it will replace the text the user typed with the text of your first completion)

    or

    Only return things in the completion list which actually math the prefix the user typed. (This is often the correct user experience.)

    - (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex
    {
       NSArray *completions = [NSArray arrayWithObjects:@"AA", @"BB", @"CC", @"DD", nil];
       NSMutableArray *filteredCompletions = [NSMutableArray array];
    
       [completions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
           if ([[obj lowercaseString] hasPrefix:[substring lowercaseString]])
               [filteredCompletions addObject:obj];
       }];
    
       return filteredCompletions;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird issue that only seems to be affecting IE 7. The
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
Strange issue that I'm not exactly sure about...also, please feel free to correct me
I have this weird issue that jquery.load sometimes fails on chrome. I'm not gonna
I have an issue that I would appreciate any help with. Background - I
I'm having an issue that I have not encountered before where App.configs for Windows
I ran into an issue that can only be explained with my fundamental lack
I have a weird issue that only seems to show itself on IE 8
I'm having an issue that apparently many people have had as well, only what
Running into another issue that I just seem cant to find any information what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.