读书人

惯用ios程序代码

发布时间: 2012-09-19 13:43:54 作者: rapoo

常用ios程序代码

?

我的原文在evernote,evernote链接的内容会不断更新

http://www.evernote.com/shard/s20/sh/9c35c399-4f7c-4015-944c-ad055cb547db/ba84af5872b716b59f18944a9a5e41a2

?

Settings 设置问题
1. setting面板的设置只有当用户去了设置面板后才会产生,所以程序要假设用户没有去过的时候设置默认值
2. 拿到[NSUserDefaultsstandardUserDefaults];取值之前要 [userDefault synchronize]
3. 假如从设置面板取回的是bool值,当你用代码 ?

[userDefault boolForKey:@"mapautozoom"];

做判断的话, 假如得到了NO, 这时你无法判断 1.是用户设置了NO 2.还是用户从来没设置过所以是NO
这时要先用个 ?
if([userDefault objectForKey:@"mapautozoom"]){
// 进来了说明用户设置过 然后再用? boolForKey
}

url encoding


NSString *urlString = [stringTobeEncoding? stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

?

alertView

??? UIAlertView *alertView = [[UIAlertView alloc]

????????????????????????????? initWithTitle:@"Error" message:message delegate:nil

????????????????????????????? cancelButtonTitle:@"OK" otherButtonTitles:nil];

??? [alertView show];

??? [alertView release];

??? [self dismissModalViewControllerAnimated:YES];





为UI添加处理事件方法


??????? UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeContactAdd];

??????? [rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];



检测用户locale ?


//这里实际检测出的是region format设定? 不是language设定


CFLocaleGetIdentifier(CFLocaleCopyCurrent())????? //zh_CN

[[NSLocaleautoupdatingCurrentLocale] localeIdentifier? //zh_CN]



添加国际化


1. 代码中使用字符串的地方 使用? NSLocalizedString(@"keyName",@"comment")

2. genstrings 扫描 .m 文件 ,生成? Localizable.strings
3. 在项目目录里生成en.lproj zh-Hans.lproj文件夹, 把Localizable.strings 放进去
4. 把Localizable.strings添加成项目的资源文件(copy bundle resources),确保打包的时候会被copy进去, ?


程序名国际化


en.lproj/InfoPlist.strings 添加

CFBundleName="EnglishName";? ?

CFBundleDisplayName="EnglishName";



Settings.bundle 国际化


Settings.bundle/en.lproj/Root.strings
"version"="Version";
Root.plist的 title写 version, 显示的就是Version



剪贴板


得到剪贴板
UIPasteboard *pasteboard=[UIPasteboard generalPasteboard];

把一个字符串放置到剪贴板上: //用户此后在输入框 粘贴的就是这个字符串
pasteboard.string = @"Hello World";


从剪贴板获取字符串:
UIPasteboard *pasteboard=[UIPasteboard generalPasteboard]; ?
NSString *myString=pasteboard.string;



UISegmentedControl


绑定? value changed? 事件




long press事件


??? UILongPressGestureRecognizer *lpress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPress:)];
??? lpress.minimumPressDuration = 0.5;//按0.5秒响应longPress方法
??? lpress.allowableMovement = 10.0;
??? [mapaddGestureRecognizer:lpress];//m_mapView是MKMapView的实例
??? [lpress release];



- (void)longPress:(UIGestureRecognizer*)gestureRecognizer
{


??? if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
??????? // put your logic here which executed when the user release their finger
??????? return;
??? }
?? ?






modal 窗口


MainVC.m
- (IBAction) helpButtonClick:(id)sender{
??? HelpVC *help= [[[HelpVCalloc]init] autorelease];
??? help.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
??? help.delegate = self;
??? [selfpresentModalViewController:help animated:YES];
?? ?
?? ?
}


- (void)dismissModal:(HelpVC *)helpVC{
??? [selfdismissModalViewControllerAnimated:YES];
}





HelpVC.h


#import <UIKit/UIKit.h>


@classHelpVC;


@protocol HelpVCDelegate


- (void)dismissModal:(HelpVC *)helpVC;


@end




@interface HelpVC : UIViewController {
?? ?
}


@property (nonatomic, assign)? id<HelpVCDelegate> delegate;


- (IBAction) closeClick:(id)sender;


@end



HelpVC.m
- (IBAction) closeClick:(id)sender{
??? [delegatedismissModal:self];
}






读书人网 >操作系统

热点推荐