你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發之——UITextField限制字數

IOS開發之——UITextField限制字數

編輯:IOS開發綜合

在輸入東西的時候,如果想限制最大字數,可以用下面方法:


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

{

    if ([string isEqualToString:@"\n"]){

        return YES;

    }

   
    NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if (self.searchTextField == textField)

    {

        if ([aString length] > 5) {

            textField.text = [aString substringToIndex:5];           

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil

                                                             message:@"超過最大字數不能輸入了"

                                                            delegate:nil

                                                   cancelButtonTitle:@"Ok"

                                                   otherButtonTitles:nil, nil];

            [alert show];

            [alert release];

            return NO;

        }

    }

    return YES;

 

}

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved