Set the maximum character length of a UITextField

前端 未结 30 2238
难免孤独
难免孤独 2020-11-22 02:27

How can I set the maximum amount of characters in a UITextField on the iPhone SDK when I load up a UIView?

30条回答
  •  我在风中等你
    2020-11-22 02:55

    Using Interface builder you can link and get the event for "Editing changed" in any of your function. Now there you can put check for the length

    - (IBAction)onValueChange:(id)sender 
    {
        NSString *text = nil;
        int MAX_LENGTH = 20;
        switch ([sender tag] ) 
        {
            case 1: 
            {
                text = myEditField.text;
                if (MAX_LENGTH < [text length]) {
                    myEditField.text = [text substringToIndex:MAX_LENGTH];
                }
            }
                break;
            default:
                break;
        }
    
    }
    

提交回复
热议问题