2011-06-16 39 views
1

我正在尝试执行阿拉伯文 datepicker。经过一个小小的研究(并没有发现),我发现这是唯一的解决方案:(我是对吗?)在UIPickerView中自定义阿拉伯文日期选择器

创建一个自定义UIPickerView与3个组件的日子,月和年。

真正的问题是我想设置一个最大日期,例如[NSDate date]

我必须将它全部编码,或者有一个简单的方法来做到这一点...任何事情都会有所帮助。

谢谢。

回答

1

由于提出的方法马杜是不一般,我终于做了我自己阿拉伯语日期选择器。

这些是.h和.m文件,有朝一日可能会帮助别人......这些文件的一个实例作为子视图添加到包含各种其他单元格类型的另一个视图。

该代码非常混乱,并可能包含超级丑陋和深韭菜,但它的工作(现在)。

dateCell.h:

#import <UIKit/UIKit.h> 


@interface dateCell : UIViewController<UIActionSheetDelegate,UIPickerViewDelegate,UIPickerViewDataSource> { 

    IBOutlet UILabel *l; 
    IBOutlet UITextField *t; 
    IBOutlet UIButton *b; 

    UIPickerView *picker; 
    UIActionSheet *actionSheet; 

    NSDictionary *numbers; 
    NSDictionary *months; 
    int year; 
    int month; 
    int day; 

    //properties 
    NSString *str; 
    NSString *name; 
    NSString *limit; 
    BOOL save; 

} 

@property (nonatomic, retain) IBOutlet UILabel *l; 
@property (nonatomic, retain) IBOutlet UITextField *t; 
@property (nonatomic, retain) IBOutlet UIButton *b; 

@property (nonatomic, retain) NSString *str; 
@property (nonatomic, retain) NSString *name; 
@property (nonatomic, retain) NSString *limit; 
@property (nonatomic) BOOL save; 
@property (nonatomic, retain) UIView* target; 
@property (nonatomic, retain) NSDictionary *numbers; 
@property (nonatomic, retain) NSDictionary *months; 
@property (nonatomic, retain) UIPickerView *picker; 
@property (nonatomic, retain) UIActionSheet *actionSheet; 

-(IBAction) bPressed; 
-(void) setTarget:(UIView *)tar; 
-(void)showPicker; 
-(int)getYear; 
-(NSString*)translateYear:(int)iYear; 
-(int) getDay; 
-(int) getMonth; 
-(int)getNumberOfDaysinYear:(int)y andMonth:(int)m; 
- (void)changeDateInLabel:(id)sender; 

@end 

dateCell.m:

#import "dateCell.h" 


@implementation dateCell 
@synthesize l,t,b; 
@synthesize name, limit, save,str,target,numbers,months,picker,actionSheet; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

-(void)loadView{ 
    [super loadView]; 
    l.text=str; 


    NSArray *aDays=[[[NSArray alloc] initWithObjects:@"١",@"٢",@"٣",@"٤",@"٥",@"٦",@"٧",@"٨",@"٩",@"٠",nil] autorelease]; 
    NSArray *eDays=[[[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0",nil] autorelease]; 
    numbers=[[NSDictionary alloc] initWithObjects:aDays forKeys:eDays]; 

    NSArray *aMonths=[[[NSArray alloc] initWithObjects:@"يناير",@"فبراير",@"مارس",@"أبريل",@"مايو",@"يونيو",@"يولي",@"أغسطس",@"سبتمبر",@"أكتوبر",@"نوفمبر",@"ديسمبر",nil] autorelease]; 
    NSArray *eMonths=[[[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",nil] autorelease]; 
    months=[[NSDictionary alloc] initWithObjects:aMonths forKeys:eMonths]; 

} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
    [l release]; 
    [t release]; 
    [b release]; 
    [name release]; 
    [limit release]; 
    [str release]; 
    [target release]; 
    [numbers release]; 
    [months release]; 
    [picker release]; 
    [actionSheet release]; 
} 


/*§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~ 
            Controls 
§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~*/ 

-(IBAction) bPressed{ 
    [self showPicker]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self showPicker]; 
    [super touchesBegan:touches withEvent:event]; 
} 

/*§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~ 
            picker handeling methods 
§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~*/ 

-(void) setTarget:(UIView *)tar{ 
    target=tar; 
} 

-(void)showPicker{ 
    actionSheet = [[UIActionSheet alloc] initWithTitle:@"select a date" 
               delegate:self 
            cancelButtonTitle:nil 
           destructiveButtonTitle:nil 
            otherButtonTitles:nil]; 

    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,40,0,0)]; 
    picker.delegate = self; 
    picker.showsSelectionIndicator = YES; 
    year=[self getYear]; 
    month=[self getMonth]; 
    day=[self getDay]; 
    [picker selectRow:year inComponent:0 animated:NO]; 
    [picker selectRow:(day+(9300/2)) inComponent:2 animated:NO]; 
    [picker selectRow:(month-1+(9600/2)) inComponent:1 animated:NO]; 
    [actionSheet addSubview:picker]; 

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"OK"]]; 
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f); 
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar; 
    closeButton.tintColor = [UIColor blackColor]; 
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged]; 
    [actionSheet addSubview:closeButton]; 
    [closeButton release]; 

    [actionSheet showInView:[UIApplication sharedApplication].keyWindow]; 
    [actionSheet setBounds:CGRectMake(0,0,320,469)]; 
    actionSheet.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin 
    |UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin; 
    [picker release]; 
    [actionSheet release]; 
} 


- (void)changeDateInLabel:(id)sender{ 
    NSString *m = [[[NSString alloc] initWithFormat:@"%d", (month-1)] autorelease]; 
    m=[months objectForKey:m]; 
    NSString *y=[self translateYear:year]; 
    NSString *d=[self translateYear:day]; 
    NSString *dat = [NSString stringWithFormat:@"%@/%@/%@",d,m,y]; 
    t.text=dat; 

    NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
    [df setDateFormat:@"yyyy-MM-dd"]; 
    NSString *realStr=[[[NSString alloc] initWithFormat:@"%d-%d-%d",year,month,day+1]autorelease]; 
    NSDate *realDate = [df dateFromString:realStr]; 
    [df release]; 
    NSLog(@"Date: %@", realDate); 
    NSString *timestamp = [NSString stringWithFormat:@"%0.0f", [realDate timeIntervalSince1970]]; 
    NSLog(@"millis: %@", timestamp); 
} 


-(void)dismissActionSheet{ 
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
} 


/*§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~ 
            Date Methods 
§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~*/ 

-(int) getYear{ 
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; 
    return [components year]; 
} 

-(NSString*)translateYear:(int)iYear{ 
    NSString *[email protected]""; 
    while(iYear>0){ 
     int r=iYear%10; 
     iYear=iYear/10; 
     NSString *s = [NSString stringWithFormat:@"%d", r]; 
     NSString *temp=[numbers objectForKey:s]; 
     aYear=[temp stringByAppendingString:aYear]; 
    } 
    return aYear; 
} 

-(int) getDay{ 
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; 
    return [components day];  
} 

-(int) getMonth{ 
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]]; 
    return [components month]; 
} 

-(int)getNumberOfDaysinYear:(int)y andMonth:(int)m{ 
    NSCalendar* cal = [NSCalendar currentCalendar]; 
    NSDateComponents* comps = [[[NSDateComponents alloc] init] autorelease]; 

    [comps setMonth:m]; 
    [comps setYear:y]; 

    NSRange range = [cal rangeOfUnit:NSDayCalendarUnit 
           inUnit:NSMonthCalendarUnit 
          forDate:[cal dateFromComponents:comps]]; 
    return range.length; 
} 


/*§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~ 
           picker delegate methods 
§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~§~*/ 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{ 
    return 3; 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 
    int d=[self getDay]; 
    int y=[self getYear]; 
    int m=[self getMonth]; 
    day=[picker selectedRowInComponent:2]%31; 
    if(day==0){ 
     day=31; 
    } 
    month=([picker selectedRowInComponent:1]%12)+1; 
    year=[picker selectedRowInComponent:0]; 
    if(year>y){ 
     year=y; 
     [picker selectRow:y inComponent:0 animated:YES]; 
     [picker selectRow:(d+(9300/2)) inComponent:2 animated:YES]; 
     [picker selectRow:(m-1+(9600/2)) inComponent:1 animated:YES]; 
    } 
    else{ 
     if(month>m && year==y){ 
      month=m; 
      [picker selectRow:(m-1+(9600/2)) inComponent:1 animated:YES]; 
      [picker selectRow:(d+(9300/2)) inComponent:2 animated:YES]; 
     } 
     else{ 
      int total=[self getNumberOfDaysinYear:year andMonth:month]; 
      if((day>total)||(day>d && month==m && year==y)){ 
       day=d; 
       [picker selectRow:(d+(9300/2)) inComponent:2 animated:YES]; 
      } 
     } 
    } 
    [picker reloadComponent:0]; 
    [picker reloadComponent:1]; 
    [picker reloadComponent:2]; 
    [self changeDateInLabel:nil]; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 
    switch (component) { 
     case 0:{ 
      return 4000; 
      break; 
     } 
     case 1:{ 
      return 12*800; 
      break; 
     } 
     case 2:{ 
      return 31*300; 
      break; 
     } 
     default:{ 
      return 0; 
      break; 
     } 
    } 
} 

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 
    UILabel *pickerLabel = (UILabel *)view; 
    if ((pickerLabel == nil) || ([pickerLabel class] != [UILabel class])) { //newlabel 
     CGRect frame = CGRectMake(0.0, 0.0, 75, 32.0); 
     pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease]; 
     pickerLabel.textAlignment = UITextAlignmentRight; 
     pickerLabel.backgroundColor = [UIColor clearColor]; 
     pickerLabel.font = [UIFont boldSystemFontOfSize:22]; 
    } 
    switch (component) { 
     case 0:{ 
      [pickerLabel setText:[self translateYear:row]];   
      int y=[self getYear]; 
      if(row==y){ 
       pickerLabel.textColor = [UIColor blueColor]; 
      } 
      else{ 
       if(row>y){ 
        pickerLabel.textColor = [UIColor grayColor]; 
       } 
       else{ 
        pickerLabel.textColor = [UIColor blackColor]; 
       } 
      } 
      return pickerLabel; 
      break; 
     } 
     case 1:{ 
      NSString *sM = [NSString stringWithFormat:@"%d", (row%12)]; 
      [pickerLabel setText:[months objectForKey:sM]]; 
      int m=[self getMonth]; 
      int y=[self getYear]; 
      if(y<=year){ 
       if(((row%12)+1)==m){ 
        pickerLabel.textColor = [UIColor blueColor]; 
       } 
       else{ 
        if(((row%12)+1)>m){ 
         pickerLabel.textColor = [UIColor grayColor]; 
        } 
        else{ 
         pickerLabel.textColor = [UIColor blackColor]; 
        } 
       } 
      } 
      else{ 
       if(year<y){ 
        if(((row%12)+1)==m){ 
         pickerLabel.textColor = [UIColor blueColor]; 
        } 
        else{ 
         pickerLabel.textColor = [UIColor blackColor]; 
        } 
       } 
      } 
      return pickerLabel; 
      break; 
     } 
     case 2:{ 
      int r; 
      if((row%31)==0){ 
       r=31; 
      } 
      else{ 
       r=(row%31); 
      } 
      [pickerLabel setText:[self translateYear:r]]; 
      int d=[self getDay]; 
      int y=[self getYear]; 
      int m=[self getMonth]; 
      if(y<=year && m<=month){ 
       if(r==d){ 
        pickerLabel.textColor = [UIColor blueColor]; 
       } 
       else{ 
        if(r>d){ 
         pickerLabel.textColor = [UIColor grayColor]; 
        } 
        else{ 
         int total=[self getNumberOfDaysinYear:year andMonth:month]; 
         if(r<=total){ 
          pickerLabel.textColor = [UIColor blackColor]; 
         } 
         else{ 
          pickerLabel.textColor = [UIColor grayColor]; 
         } 
        } 
       } 
      } 
      else{ 
       if(year<y || month<m){ 
        if(r==d){ 
         pickerLabel.textColor = [UIColor blueColor]; 
        } 
        else{ 
         int total=[self getNumberOfDaysinYear:year andMonth:month]; 
         if(r<=total){ 
          pickerLabel.textColor = [UIColor blackColor]; 
         } 
         else{ 
          pickerLabel.textColor = [UIColor grayColor]; 
         } 
        } 
       } 
      } 
      return pickerLabel; 
      break; 
     } 
    } 
    return nil; 
} 




@end 

谢谢。

0

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
               delegate:self 
            cancelButtonTitle:@"Cancel" 
           destructiveButtonTitle:nil 
            otherButtonTitles:nil]; 

// Add the picker 
UIDatePicker *pickerView = [[UIDatePicker alloc] init]; 
pickerView.datePickerMode = UIDatePickerModeDate; 

//urmindate,urmaxdate are NSDate objects 
[pickerView setMinimumDate:urmindate]; 

[pickerView setMinimumDate:urmaxdate]; 

[menu addSubview:pickerView]; 
[menu showInView:self.view];   
[menu setBounds:CGRectMake(0,0,320, 500)]; 

CGRect pickerRect = pickerView.bounds; 
pickerRect.origin.y = -100; 
pickerView.bounds = pickerRect; 

[pickerView release]; 
[menu release]; 

http://developer.apple.com/library/mac/#documentation/cocoa/reference/Foundation/Classes/NSLocale_Class/Reference/Reference.html

的语言环境属性(以及与此有关的所有其他国家特定的格式属性)默认为通过currentLocale,这又取决于返回的值在iPhone的国家设置,而不是语言设置。您需要在iphone常规设置中设置适当的国家/地区。

+0

此代码为您提供标准日期的“UIDatePicker”,而不是阿拉伯语... – astazed 2011-06-16 09:17:40

+0

有没有一种方法来以编程方式设置它? – astazed 2011-06-16 10:06:59

相关问题