Translate

2012年12月19日 星期三

Xcode4.2 本地化多國語言Localizable跟AppleLanguages

1.再Supporting Files文件夾點右鍵,New File->iOS->Resource->Strings File取名為Localizable

2.選中Localizable.strings 再回Xcode->View->Utilites->File Inspector,再Localization中點擊+添加語言    比如中文英文日文

3.
Localizable.strings(English)
"test"=test";
Localizable.strings(Chinese)
"test="測試";

4.使用方法
[temp_lab setText:NSLocalizedString(@"test", @"測試用的")];
誇號裡第一個參數是要顯示的內容,與個Localozable.strings中的id對應
第二個參數是對第一個參數的註解,一般可以為空字串

另外也可以用程式取得使用者的語系
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; 
NSLog(@"%@", languages);

2012年12月7日 星期五

how to call shouldautorotatetointerfaceorientation

這個問題是我一個朋友問我的
覺得有可能需要紀錄下來

朋友的問題是
一開始假設機器是橫的
然後我拖動畫面~把開關設成NO~然後轉機器~這時候到了shouldAutorotateToInterfaceOrientation會因為我的布林值而不去旋轉畫面~但是事實上機器已經被設定成直式了
然後我放開後再去檢查機器的方向~這時候我會查到機器是直的~然後做直的layout~但是機器的畫面卻是橫的
所以畫面就整個不對了

最後結論就是
簡單來說就是怎麼去重新觸發shouldAutorotateToInterfaceOrientation

If you are target iOS 5, you can call

[UIViewController attempRotationToDeviceOrientation

which will can shouldAutorotateToInterfaceDeviceOrientation: with the device's current orientation and will rotate the interface if that method returns YES.

參考
http://stackoverflow.com/questions/9051192/iphone-change-orientation-manually

解決囉

2012年9月25日 星期二

NSArray compare 排序

其實一般NSArray的排序

NSArray *sortedArray = [temp_dic_dic sortedArrayUsingSelector:@selector(compare:)]; for(int i =0; i < [sortedArray count]; i++) { int x = [[sortedArray objectAtIndex:i] intValue]; NSLog(@"x is %d",x); }

但是我需要的是針對NSArray多個dictionary,每個dictionary都有個key值為"dddNumber",key值的形態為字串,如何依照NSString比對呢?

NSMutableArray *array = [[NSMutableArray alloc] init]; for (int i = 0; i < 10; i ++) { NSDictionary *aDict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"akdj%i", i], @"phaseNumber", nil]; [array addObject:aDict]; [aDict release]; } // 排序 NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"dddNumber" ascending:YES] autorelease]; NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
是要用intValue排序的话。上面代码是不对的,上面是按照String来排序的。 如果要按照intValue排序,参见如下:

方法
NSComparisonResult compare(NSDictionary *firstDict, NSDictionary *secondDict, void *context) {NSLog(@"IN(%i, %i)", [[firstDict objectForKey:@"ddd"] intValue], [[secondDict objectForKey:@"ddd"] intValue]); if ([[firstDict objectForKey:@"ddd"] intValue] < [[secondDict objectForKey:@"ddd"] intValue]) return NSOrderedAscending; else if ([[firstDict objectForKey:@"ddd"] intValue] > [[secondDict objectForKey:@"ddd"] intValue]) return NSOrderedDescending; else return NSOrderedSame; } [array sortUsingFunction:compare context:NULL];
使用
[array sortUsingFunction:compare context:NULL];

2012年9月5日 星期三

Application does not run in background

在專案的info.plist新增

Application does not run in background

設為YES


Where is the XCode 4 build directory?


Where is the XCode 4 build directory?

In all previous versions, XCode used to create a build directory inside the project folder and the executables were saved in that location.

However, XCode 4 changed all that.
Infact, I spent quite sometime searching for my executables.
XCode 4 uses the derived data location for storing your builds by default.

Just click on the XCode menu on the top bar, and select Preferences -> Locations tab.
Here under build location, you can see the default setting selected as "place build products in derived data location (recommended)".

And if you check the "Derived Data Location" section of this tab you will see the default location is /Users/..../Library/Developer/XCode/DerivedData.
You will see a small arrow next to this path and clicking on it takes you directly to this location and voila there is your build directory!

2012年8月29日 星期三

NSDateFormatter


部分日期格式的总结:
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setAMSymbol:@"AM"];
[dateFormatter setPMSymbol:@"PM"];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mmaaa"];
NSDate *date = [NSDate date];
NSString * s = [dateFormatter stringFromDate:date];
显示效果为:10/05/2010 03:49PM

下面附上几个格式:这个是从Cocoachina找来的:
yyyy:MM:dd G 'at' HH:mm:ss zzz            1996.07.10 AD at 15:08:56 PDT
EEE, MMM d, "yy                                         Wed,july 10, '99
h:mm a                                                     12:08 PM
hh 'o"clock' a,zzzz                                      12 o'clock PM, Pacific Daylight Time
K:mm a, z                                                 0:00 PM, PST
yyyyy,MMMM.dd GGG hh:mm aaa                01996.july.10 AD 12:08 PM

下面是得到当前的年,月,日,时,分,秒
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dd = [cal components:unitFlags fromDate:date];
int y = [dd year];
int m = [dd month];
int d = [dd day];
int h = [dd hour];
int m = [dd minute];
int s = [dd second];
日期格式代表意義
yyyy = 年(4位數), yy = 年(最後2位數),MM = 月(1~12),MMM = 月(英文縮寫),MMMM = 月(英文完整)
K = 時(12小時制),H = 時(24小時制),mm = 分,ss = 秒,aa = 午前/午後(AM/PM)
dd = 日,EEE = 星期(英文縮寫),EEEE = 星期(英文完整)

2012年8月22日 星期三

UIWebView backgroundColor

web.backgroundColor = [UIColor clearColor]; web.opaque = NO; 关键是在HTML里: