English 中文(简体)
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.





相关问题
How can I load a folders files into a ListView?

I d like to have a user select a folder with the FolderBrowserDialog and have the files loaded into the ListView. My intention is to make a little playlist of sorts so I have to modify a couple of ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Saving output of a for-loop to file

I have opened a file with blast results and printed out the hits in fasta format to the screen. The code looks like this: result_handle = open("/Users/jonbra/Desktop/my_blast.xml") from Bio.Blast ...

热门标签