3

UISegmentedControl对使用情节提要有一个看法,目前正在使用以下代码行以编程方式设置文本:

[segMonth setTitle:@"Month 1" forSegmentAtIndex:0];
[segMonth setTitle:@"Month 2" forSegmentAtIndex:1];

我还有一个使用此代码的日期函数,它获取当前月份的数字(1-12):

// Date
    NSDate *now = [NSDate date];
    NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now];
    NSArray *arr = [strDate componentsSeparatedByString:@" "];
    NSString *str;
    str = [arr objectAtIndex:0];

    NSArray *arr_my = [str componentsSeparatedByString:@"-"];

    NSInteger month = [[arr_my objectAtIndex:1] intValue];
//End Date

我试图将当前月份的第一段命名为“十二月”,将下个月的第二段命名为“一月”。

我尝试使用以下代码,但它似乎不起作用:

[segMonth setTitle:@"%d" forSegmentAtIndex:0];
[segMonth setTitle:@"%d" forSegmentAtIndex:1];

显然,这也只会给出月份的数字,而不是名称..

4

4 回答 4

1

您正在传递一个字符串格式化程序并期望它包含月份的名称。尝试

[segMonth setTitle:[NSString stringWithFormat:@"%@",str1] forSegmentAtIndex:0];
[segMonth setTitle:[NSString stringWithFormat:@"%@",str2] forSegmentAtIndex:1];

str1 和 str2 应该是包含月份名称的字符串(可通过 NSDateFormatter 获得)。

于 2012-12-29T13:11:15.173 回答
1

从这样的数字中获取月份名称:

 NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
 NSString *monthName = [[df monthSymbols] objectAtIndex:(yourMonthNumberHere-1)];

现在使用它:

 [segMonth setTitle:monthName forSegmentAtIndex:0];
于 2012-12-29T13:17:28.357 回答
1

我看过你的代码,其中包含一个很难从NSDate我知道的月份中获得一个月的方法,这可能不是答案。但我只是要求您检查此代码,以了解获取月份、日期或时间或任何与NSDate.

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSMonthCalendarUnit fromDate:today];
NSInteger currentMonth = [components month]; // this will give you the integer for the month number

[components setMonth:1];
NSDate *newDate = [gregorian dateByAddingComponents:components toDate:today options:0];
NSDateComponents *nextComponents = [gregorian components:NSMonthCalendarUnit fromDate:newDate];

更新:

NSInteger nextMonth = [nextComponents month]; // this will give you the integer for the month number

正如@Prince 所说,您可以从NSDateFormatter. 无论如何,我重复一遍让你明白。

NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSString *currentMonthName = [[df monthSymbols] objectAtIndex:(currentMonth-1)];
NSString *nextMonthName = [[df monthSymbols] objectAtIndex:(nextMonth-1)];

[segMonth setTitle:currentMonthName forSegmentAtIndex:0];
[segMonth setTitle:nextMonthName forSegmentAtIndex:1];
于 2012-12-29T13:55:58.623 回答
0
int currentMonth = 12;   //December
int nextMonth = 1;   //January
NSDateFormatter *df1 = [[[NSDateFormatter alloc] init] autorelease];
NSString *currentMonthName = [[df1 monthSymbols] objectAtIndex:(currentMonth-1)];

NSDateFormatter *df2 = [[[NSDateFormatter alloc] init] autorelease];
NSString *nextMonthName = [[df2 monthSymbols] objectAtIndex:(nextMonth-1)];

[segMonth setTitle:currentMonthName forSegmentAtIndex:0];
[segMonth setTitle:nextMonthName forSegmentAtIndex:1];

请注意,您需要从 currentMonth 和 nextMonth(从 1 到 12)中减去 1,因为 monthSymbols 是从零开始的。

于 2012-12-29T13:52:07.363 回答