xcode object-c 笔记
[plain]/*
1. IBOutlet inte易做图ce builder 为了使用 inte易做图ce 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 File
3.将文件保存名设置为Localizable.strings
4.在Localizable.strings 文件上按右键并选择 Get Info
5.点击信息界面的Make File Localizable,然后再将Tab标签切换到General
6.输入新的语言名称 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/*.m
genstrings -o zh.lproj ./classes/*.m
五、编辑各Localizable.strings文件
从第四步中得到了与代码对应的资源文件,最后我们需要对这些资源文件翻译成对应的语言就可以了.如在Localizable.strings(zh)中, 把等号后的文字进行编译成中文.
"Submitted successfully" = "提交成功"
重新编译整个工程后,就会在不同的语言环境下得
*/
/*
9. NSString and NSMutableString
*/
use @"abc" to mean NSString
ex: NSString *str = @"Hello";
use content of file to create NSString
ex: NSString *str = [NSString stringWithContentsOfFile:@"/path/to/file"]
use c characters to create NSString
ex: char *cStr="hello";
NSString *str = [NSString stringWithCString: cStr]
get length of NSString
ex: unsigned int strLen = [str length]
append on NSString to another
ex: 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方法 ----------------*/
补充:综合编程 , 其他综合 ,