iPhoneNScalendar-私たちの外のユーザー向けに描かれた誤ったカレンダー

StackOverflow https://stackoverflow.com/questions/5334185

  •  26-10-2019
  •  | 
  •  

質問

他の誰かのコードや非常に多くの異なる反復に取り組もうとした後、暦月ビューで日付で何が間違っているのか理解できません。私はカレンダーのようなカレンダーでカレンダーを提示しようとしています。たとえば、オーストラリアでは、3月17日金曜日に表示されます。どんな助けも高く評価されています!

これが私が使用しているコードサンプルです。

-(void)setCalendar
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:units fromDate:[NSDate date]];
    self.currentDate = [gregorian dateFromComponents:comps];
    NSInteger weekdayInteger=[comps weekday];
    currentMonth=[comps month]; 
    currentYear=[comps year];   
    NSString *dateString=[[NSString stringWithFormat:@"%@",currentDate] substringToIndex:10];
    NSArray *array= [dateString componentsSeparatedByString:@"-"];
    NSInteger currentDay=[[array objectAtIndex:2] intValue];
    currentMonthConstant=currentMonth;
    currentYearConstant=currentYear;
    currentDayConstant=currentDay;
    currentConstantDate=[NSString stringWithFormat:@"%d/%d/%d",currentDayConstant,currentMonthConstant,currentYearConstant];
    currentFirstDay=((8+weekdayInteger-(currentDay%7))%7);
    if(currentFirstDay==0)
        currentFirstDay=7;
    [gregorian release];
}

そして、これがカレンダーにどのように埋め込まれていますか:

-(void)fillCalender:(NSInteger)month weekStarts:(NSInteger)weekday year:(NSInteger)year
{

    currentMonth=month;
    currentYear=year;
    currentFirstDay=weekday;
    UIButton *temp;
    NSInteger numberOfDays=[self getNumberofDays:month YearVlue:year];
    currentLastDay=(currentFirstDay+numberOfDays-1)%7;
    if(currentLastDay==0)
        currentLastDay=7;
    NSString *monthName=[self getMonth:month];
    NSInteger grid=(weekday-1); 

    monthLabel.text=[NSString stringWithFormat:@"%@   %d",monthName,year];


    for(int i=1;i<=numberOfDays;i++)
    {
        if([frontView isEqualToString:@"view1"])
            temp=[arr_View2Buttons objectAtIndex:grid];
        else
            temp= [arr_View1Buttons objectAtIndex:grid];

        [temp setBackgroundImage:nil forState:UIControlStateNormal];

        [temp setUserInteractionEnabled:TRUE];
        UILabel *aLbl = (UILabel *)[temp viewWithTag:123];
        [aLbl setText:[NSString stringWithFormat:@"%d",i]];

        // check for today
        if(currentDayConstant == i && currentMonthConstant == currentMonth && currentYearConstant == currentYear)
        {
            [aLbl setTextColor:[UIColor blueColor]];
        }
        else
        {
            [aLbl setTextColor:[UIColor colorWithRed:69/255.0 green:2/255.0 blue:71/255.0 alpha:1.0]];
        }
        [temp setTag:i];

        grid++;
        if(grid==35)
            grid=0;
    }

}

そして最後にviewdidload:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setCalendar];
    // set views
    [self.view1 setFrame:CGRectMake(0,45,320,232)];
    [self.view2 setFrame:CGRectMake(0,277,320,232)];
    [self.view addSubview:self.view1];
    [self.view addSubview:self.view2];
    frontView = @"view2";
    [self fillCalender:currentMonth weekStarts:currentFirstDay year:currentYear];
    frontView = @"view1";
}
役に立ちましたか?

解決

コードを変更する必要があります

nscalendar *gregorian = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar];

nscalendar *currentCalendar = [nscalendar currentCalendar];

これにより、デバイスから現在のカレンダーが識別され、それに応じて実行されます

これがあなたの問題を解決することを願っています...

他のヒント

NSDateComponentsを使用して、平日、月、年を取得します。それは大丈夫です。
しかし、その後、文字列操作を使用してその日を取得します。
そして、NSDateの説明方法から得た文字列を操作します。そして、私の経験から、この方法はそれが望むものを何でも返します。どのタイムゾーンが説明方法を使用するかを把握することはできませんでした。私のマシンでは、UTC TimeZoneのNSDATEを返します。ローカルタイムゾーンの隣。または任意のタイムゾーンで。通常、これは問題ではありません。タイムゾーン情報を使用できるからですが、単に無視するだけです。

長い話を短くします: 日付の処理が必要な場合は、文字列処理を使用しないでください

IMHOこれはタイムゾーンの問題です。 2つの異なる日付の処理方法を組み合わせて、タイムゾーンを無視するからです。
文字列操作パーツを取り除き、すべてにNSDateComponentsを使用します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top