【问题标题】:Set UISegmentedControl text programmatically from variable从变量以编程方式设置 UISegmentedControl 文本
【发布时间】:2012-12-14 10:54:30
【问题描述】:

我使用情节提要在我的视图中有一个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];

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

【问题讨论】:

标签: ios xcode uisegmentedcontrol


【解决方案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];

【讨论】:

  • 我已经实现了您的代码,它似乎可以工作,但是,我尝试更改手机上的日期以进行测试。我把它改成了六月,第一段正确地改为六月但是第二段仍然是一月....
  • 我也得到'未使用的变量'nextComponents'
  • @Jack 我已经更新了代码并在更新的行之前添加了文本“UPDATED”..现在检查
【解决方案2】:
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 是从零开始的。

【讨论】:

  • 这只是一个演示,您必须动态获取月份值。
【解决方案3】:

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

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

现在使用它:

 [segMonth setTitle:monthName forSegmentAtIndex:0];

【讨论】:

    【解决方案4】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 2012-06-28
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多