2012-12-29 25 views
3

我有我的觀點一個UISegmentedControl用故事板和文本目前正在設定編程方式使用下面的代碼行:使用此代碼設置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]; 

顯然,這也將只給了一個月,不是名稱的數量..

+0

http://stackoverflow.com/questions/4488373/ios-how-to-get-a-proper-month-name-from-a-number –

回答

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

我已經實現你的代碼,它似乎工作,但是,我試着改變我的手機上的日期來測試。我將它改爲六月,第一個網段正確更改爲六月,但第二個網段仍然會說一月...... – Jack

+0

我還得到'未使用的變量'nextComponents' – Jack

+0

@Jack我更新了代碼,並在「更新」更新行..現在檢查 –

1

你」重新傳遞一個字符串格式化程序,並期望它包含月份的名稱。嘗試

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

str1和str2應該是包含月份名稱的字符串(可通過NSDateFormatter獲得)。從數量

1

獲取MONTHNAME這樣的:

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

現在使用它:

[segMonth setTitle:monthName forSegmentAtIndex:0]; 
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 1 - 12)因爲monthSymbols是從零開始的。

+0

難道我要每天更新代碼月? – Jack

+0

這只是一個演示,您必須動態獲取月份值。 – Shabib

相關問題