开发者

Solution for iPhone new file dialog keyboard

i want to let the user type in the name of a new file, so there are certain characters i want to prevent entry on. is there a special keyboard i can use or can i disable certain keys on the iphones keyboard.

is the answer to just run a regular expression on the input text and tell the user the filename is invalid (if so what would that regular expression be?)

ANSWER: (or what i ended up doing)

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

    BOOL valid;
    //if the user has put in a space at the beginning
    if ([string isEqualToString:@" "]){
        if (range.location == 0){
            valid = NO;
     开发者_开发知识库   }
        else{
            valid = YES;
        }
    }
    //otherwise test for alpha numeric
    else{
        NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
        valid = [[string stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""];
    }

    //print the warning label
    if (valid == NO){
        [errorLabel setText:@"Invalid input"];
    }
    else{
        [errorLabel setText:nil];
    }
    return valid;
}


You can implement the delegate method

For UITextField,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text

For UITextview

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

and decide weather to append the entered characters or not.


You can implement the UITextFieldDelegate protocol and use textField:shouldChangeCharactersInRange:replacementString: to watch the text entry and prevent unwanted characters by returning NO.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜