通过变量编程设置UISegmentedControl文本

3

我在Storyboard上的视图中使用了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];

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

https://dev59.com/wG855IYBdhLWcg3wMBLV - Dinesh Raja
4个回答

1

按照以下方式从数字获取月份名称:

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

现在使用它:

 [segMonth setTitle:monthName forSegmentAtIndex:0];

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];

我已经实现了你的代码,看起来它能够工作,但是我尝试改变我的手机日期进行测试。我将它改为六月,第一个部分正确地变成了六月,然而第二个部分仍然显示着一月... - Jack
我还得到了“未使用的变量'nextComponents'”。 - Jack
@Jack,我已经更新了代码,并在更新的行之前添加了文本“已更新”..现在请检查。 - Dinesh Raja

1
您正在传递一个字符串格式化程序,并期望其中包含月份的名称。请尝试:
[segMonth setTitle:[NSString stringWithFormat:@"%@",str1] forSegmentAtIndex:0];
[segMonth setTitle:[NSString stringWithFormat:@"%@",str2] forSegmentAtIndex:1];

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


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];

请注意,您需要从当前月份和下一个月份(从1-12)中减去1,因为monthSymbols是基于零的。

这只是一个演示,您必须动态获取月份值。 - Shabib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接