/*1. IBOutlet interface builder 为了使用 interface builder 识别*/@property (nonatimic, retain) IBOutlet UIImageView *imageview;/*2. 载入新的视图view*/FlipsideVC *VC = [[FlipsideVC alloc] initWithNibName:@"NIB名字" bundle:nil];[self.navigationController pushViewController:vc animated:YES];[vc release];/*3.载入网络图片*/NSString *imageurl = @"http://****/1.jpg";NSError *error = nil;NSURL *url = [NSURL URLWithString:imageurl];NSData *imageData = [[NSData alloc] initWithContentsOfURL:url options:NSMappedRead error:&error];UIImage *picimage = [[UIImage alloc] initWithData:imageData];[imagedata release];/*4.动态添加button,自定义的图片按钮*/UIButton *closeButton = [[UIButton alloc] initWithFrame: CGRectMake(278, -30, 60, 60)];[closeButton setBackgroundColor:[UIColor clearColor]];[closeButton setImage:[UIImage imageNamed:@"ad_close_x.png"] forState:UIControlStateNormal];[closeButton addTarget:self action:@selector(OnCloseAdButtonPressed:) forControlEvents:UIControlEventTouchUpInside];[adView addSubview:closeButton];/*5.NSAutoreleasePool 自动释放*/NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];....[pool release];/*6. NSLog 日志*/NSLog(@"aaaaaaaaaaa");/*7. 使用NSLocalizedString实现国际化*/NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; NSLog(@"%@", languages);/*8 在Xcode中建立多语言文档*//*1.在Resources分类下新建文档(右鍵/Add/New File…)2.在模板对话框中选择Other,然后再选择Strings File3.将文件保存名设置为Localizable.strings4.在Localizable.strings 文件上按右键并选择 Get Info5.点击信息界面的Make File Localizable,然后再将Tab标签切换到General6.输入新的语言名称 zh 後按 Add,些时有English与zh两种语言,你还可以增加其它语言.在源代码中使用NSLocalizedString来引用国际化文件//括号里第一个参数是要显示的内容,与各Localizable.strings中的id对应//第二个是对第一个参数的注释,一般可以为空串[_alertView setTitle:NSLocalizedString(@"Submitted successfully",@"")];四、使用Terminal的genstrings命令进行生成资源文件打开Terminal,然后cd到工程所在的目录,然后使用genstrings来生成自动从源代码中生成资源文件.例如,项目的目录为:/user/project/test01,则命令如下:genstrings -o English.lproj ./classes/*.mgenstrings -o zh.lproj ./classes/*.m五、编辑各Localizable.strings文件从第四步中得到了与代码对应的资源文件,最后我们需要对这些资源文件翻译成对应的语言就可以了.如在Localizable.strings(zh)中, 把等号后的文字进行编译成中文."Submitted successfully" = "提交成功"重新编译整个工程后,就会在不同的语言环境下得*//*9. NSString and NSMutableString*/use @"abc" to mean NSStringex: NSString *str = @"Hello";use content of file to create NSStringex: NSString *str = [NSString stringWithContentsOfFile:@"/path/to/file"]use c characters to create NSStringex: char *cStr="hello";NSString *str = [NSString stringWithCString: cStr]get length of NSStringex: unsigned int strLen = [str length]append on NSString to anotherex: NSString *str = @"Hello";NSString *str2 = [str stringByAppendingString: @"abc"]append a format:ex: NSString *str3 = [str2 stringByAppendingFormat: @"%d", 2003]search for subString:ex: NSRange loc = [str rangeOfString:@"The"]what is NSRange:typedef struct _NSRange{unsigned int location;unsigned int length;}NSRange;breaking a string into components:ex: NSArray *fields = [str componentsSeperatedByString:@"abc"];create NSMutableString from NSString:ex: NSString *str = @"hello";NSMutableString *ms = [NSMutableString stringWithString: str]; /*10. NSString+NSMutableString+NSValue+NSAraay用法汇总*//*----------------创建字符串的方法----------------*/ //1、创建常量字符串。 NSString *astring = @"This is a String!"; //2、创建空字符串,给予赋值。 NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; NSLog(@"astring:%@",astring); [astring release]; //3、在以上方法中,提升速度:initWithString方法 NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); [astring release]; //4、用标准c创建字符串:initWithCString方法 char *Cstring = "This is a String!"; NSString *astring = [[NSString alloc] initWithCString:Cstring]; NSLog(@"astring:%@",astring); [astring release]; //5、创建格式化字符串:占位符(由一个%加一个字符组成) int i = 1; int j = 2; NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]]; NSLog(@"astring:%@",astring); [astring release]; //6、创建临时字符串 NSString *astring; astring = [NSString stringWithCString:"This is a temporary string"]; NSLog(@"astring:%@",astring); /*----------------从文件读取字符串:initWithContentsOfFile方法 ----------------*/ NSString *path = @"astring.text"; NSString *astring = [[NSString alloc] initWithContentsOfFile:path]; NSLog(@"astring:%@",astring); [astring release]; /*----------------写字符串到文件:writeToFile方法 ----------------*/ NSString *astring = [[NSString alloc] initWithString:@"This is a String!"]; NSLog(@"astring:%@",astring); NSString *path = @"astring.text"; [astring writeToFile: path atomically: YES]; [astring release]; /*---------------- 比较两个字符串----------------*/ //用C比较:strcmp函数 char string1[] = "string!"; char string2[] = "string!"; if(strcmp(string1, string2) = = 0) { NSLog(@"1"); } //isEqualToString方法 NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 isEqualToString:astring02]; NSLog(@"result:%d",result); //compare方法(comparer返回的三种值) NSString *astring01 = @"This is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedSame; NSLog(@"result:%d",result); //NSOrderedSame 判断两者内容是否相同 NSString *astring01 = @"This is a String!"; NSString *astring02 = @"this is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedAscending; NSLog(@"result:%d",result); //NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真) NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02] = = NSOrderedDescending; NSLog(@"result:%d",result); //NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真) //不考虑大 小写比较字符串1 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame; NSLog(@"result:%d",result); //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为 真) //不考虑大小写比较字符串2 NSString *astring01 = @"this is a String!"; NSString *astring02 = @"This is a String!"; BOOL result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame; NSLog(@"result:%d",result); //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。 /*----------------改变字符串的大小写----------------*/ NSString *string1 = @"A String"; NSString *string2 = @"String"; NSLog(@"string1:%@",[string1 uppercaseString]);//大写 NSLog(@"string2:%@",[string2 lowercaseString]);//小写 NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小 /*----------------在串中搜索子串 ----------------*/ NSString *string1 = @"This is a string"; NSString *string2 = @"string"; NSRange range = [string1 rangeOfString:string2]; int location = range.location; int leight = range.length; NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]]; NSLog(@"astring:%@",astring); [astring release]; /*----------------抽取子串 ----------------*/ //-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符 NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringToIndex:3]; NSLog(@"string2:%@",string2); //-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符 NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringFromIndex:3]; NSLog(@"string2:%@",string2); //-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串 NSString *string1 = @"This is a string"; NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)]; NSLog(@"string2:%@",string2);/*11. 随机数的使用*/ 头文件的引用 #import <time.h> #import <mach/mach_time.h> srandom()的使用 srandom((unsigned)(mach_absolute_time() & 0xFFFFFFFF)); 直接使用 random() 来调用随机数/*12 在UIImageView 中旋转图像*/float rotateAngle = M_PI; CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle); imageView.transform = transform;/*13 在Quartz中如何设置旋转点*/UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);这个是把旋转点设置为底部中间。记住是在QuartzCore.framework中才得到支持/*14 创建.plist文件并存储*/NSString *errorDesc; //用来存放错误信息 NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:4]; //NSDictionary, NSData等文件可以直接转化为plist文件 NSDictionary *innerDict; NSString *name; Player *player; NSInteger saveIndex; for(int i = 0; i < [playerArray count]; i++) { player = nil; player = [playerArray objectAtIndex:i]; if(player == nil) break; name = player.playerName;// This "Player1" denotes the player name could also be the computer name innerDict = [self getAllNodeInfoToDictionary:player]; [rootObj setObject:innerDict forKey:name]; // This "Player1" denotes the person who start this game } player = nil; NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc]; 红色部分可以忽略,只是给rootObj添加一点内容。这个plistData为创建好的plist文件,用其writeToFile方法就可以写成文件。下面是代码: /*得到移动设备上的文件存放位置*/ NSString *documentsPath = [self getDocumentsDirectory]; NSString *savePath = [documentsPath stringByAppendingPathComponent:@"save.plist"]; /*存文件*/ if (plistData) { [plistData writeToFile:savePath atomically:YES]; } else { NSLog(errorDesc); [errorDesc release]; } - (NSString *)getDocumentsDirectory { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [paths objectAtIndex:0]; } /*15 读取plist文件并转化为NSDictionary*/NSString *documentsPath = [self getDocumentsDirectory]; NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"save.plist"]; NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];/*16 读取一般性文档文件*/NSString *tmp; NSArray *lines; /*将文件转化为一行一行的*/ lines = [[NSString stringWithContentsOfFile:@"testFileReadLines.txt"] componentsSeparatedByString:@"\n"]; NSEnumerator *nse = [lines objectEnumerator]; // 读取<>里的内容 while(tmp = [nse nextObject]) { NSString *stringBetweenBrackets = nil; NSScanner *scanner = [NSScanner scannerWithString:tmp]; [scanner scanUpToString:@"<" intoString:nil]; [scanner scanString:@"<" intoString:nil]; [scanner scanUpToString:@">" intoString:&stringBetweenBrackets]; NSLog([stringBetweenBrackets description]); }/*17 隐藏NavigationBar*/[self.navigationController setNavigationBarHidden:YES animated:YES];/*18 屏蔽iphone虚拟键盘*/- (BOOL) textFieldShouldBeginEditing: (UITextField *)textField{return NO;}/*19 让label自适应里面的文字,自动调整宽度和高度的 */UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];这个frame是初设的,没关系,后面还会重新设置其size。[label setNumberOfLines:0];NSString *s = @"string......";UIFont *font = [UIFont fontWithName:@"Arial" size:12];CGSize size = CGSizeMake(320,2000);CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];[label setFrame:CGRectMake:(0,0, labelsize.width, labelsize.height)];[self.view addSubView:label];/*20 iPhone 应用中实现拨打电话功能的代码*/面的代码能在应用中添加一个电话按钮,点击即可拨打电话号码。对于 iPhone 开发者还是很有用的。//添加电话图标按钮 UIButton *btnPhone = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; btnPhone.frame = CGRectMake(280,10,30,30); UIImage *image = [UIImage imageNamed:@"phone.png"]; [btnPhone setBackgroundImage:image forState:UIControlStateNormal]; //点击拨号按钮直接拨号 [btnPhone addTarget:self action:@selector(callAction:event:) forControlEvents:UIControlEventTouchUpInside]; [cell.contentView addSubview:btnPhone]; //cell是一个UITableViewCell//定义点击拨号按钮时的操作- (void)callAction:(id)sender event:(id)event{ NSSet *touches = [event allTouches]; UITouch *touch = [touches anyObject]; CGPoint currentTouchPosition = [touch locationInView:self.listTable]; NSIndexPath *indexPath = [self.listTable indexPathForRowAtPoint: currentTouchPosition]; if (indexPath == nil) { return; } NSInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSDictionary *rowData = [datas objectAtIndex:row]; NSString *num = [[NSString alloc] initWithFormat:@"tel://%@",number]; //number为号码字符串 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号}/*21 获取网页 HTML 中 <Title>内容的代码*/涉及到互联网的 iPhone 应用里往往要抓取网页的<title>内容,您可以用 UIWebView,加载完成后执行 javascript 取得 html 的 titleself.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];/*22 NSString中用一个字符替换NSString中某个特别的字符*/在NSString中,需要用一个字符代替NSString字符串里面的某个特别的字符,此时使用[NSString stringByReplacingOccurrencesOfString: withString:];而在string中,需要用一个字符代替string字符串里面的某个特别的字符,此时使用[string replaceOccurrencesOfString:(NSString *) withString:(NSString *)][string replaceOccurrencesOfString:@"A" withString:@"B" options:NSCaseInsensitiveSearch range:NSRange){0,[string length]}];/*23 category*/category 就是对原有的类进行一个功能扩展,只扩展方法,不能扩展成员变量/*24 blocks 函数指针*/typedef int (^sumblockT) (int a, int b);sumblockT block = ^(int a, int b) {return a+b;}__block int sum = 0; //使用__block 使其成为全局变量void (^myblock) (int a, int b) = ^(int a, int b) {int c = a + b;sum = a + b;return c;};/*25 protocal 协议,使用协议进行继承*/@protocal aaaa@end