Reputation: 585
Is there a way to make sure the user is only able to enter dates in a textfield? For example, characters 0-9 and / (or a better solution)? If so, what would be the best way of doing this?
I was unsuccessful with
NSString *LEGAL = @"0123456789/";
NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:LEGAL] invertedSet];
NSString *filteredOne = [[firstString componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:@""];
NSString *filteredTwo = [[secondString componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:@""];
firstString = filteredOne;
secondString = filteredTwo;
Upvotes: 0
Views: 1234
Reputation: 6522
If I understand your question correctly, I might be able to help. Here is some old code I've used before to format textfields to select dates. Hopefully you are able to understand it and adapt it to your needs:
Note: UITextField *endDate;
//Get date and format it for the textfields
-(void)updateTextField:(id)sender
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
UIDatePicker *picker = (UIDatePicker*)self.endDate.inputView;
NSString *theDate = [dateFormat stringFromDate:picker.date];
self.endDate.text = [NSString stringWithFormat:@"%@",theDate];
[dateFormat release];
}
To have a date selector instead of a keyboard, try something like this in the viewDidLoad() method*
//Make date pickers instead of keyboards for time text boxes
UIDatePicker *datePicker = [[[UIDatePicker alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 162.0)] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.maximumDate = [NSDate date];
NSDate *setDate = [NSDate dateWithYear: 2012 month: 02 day: 03];
[datePicker setDate:setDate];
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
[self.startDate setInputView:datePicker];
datePicker = [[[UIDatePicker alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 162.0)] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.maximumDate = [NSDate date];
setDate = [NSDate dateWithYear: 2012 month: 02 day: 10];
[datePicker setDate:setDate];
[datePicker addTarget:self action:@selector(updateTextField2:) forControlEvents:UIControlEventValueChanged];
[self.endDate setInputView:datePicker];
*Note, you may need your own NSDate interface/implementation, if so, the code is below. Don't forget to import it at the top of your file!
NSDate.h
#import <Foundation/Foundation.h>
@interface NSDate (missingFunctions)
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day;
@end
NSDate.m
#import "NSDate.h"
@implementation NSDate (missingFunctions)
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:year];
[components setMonth:month];
[components setDay:day];
return [calendar dateFromComponents:components];
}
@end
Upvotes: 1