iPhone开发中的技巧整理(二)
1、NSCalendar用法-(NSString *) getWeek:(NSDate *)d
{
NSCalendar *calendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit | NSWeekCalendarUnit;
NSDateComponents *components = [calendar components:units
fromDate:d];
[calendar release];
switch ([components weekday]) {
case1:
return @"Monday"; break;
case2:
return @"Tuesday"; break;
case3:
return @"Wednesday"; break;
case4:
return @"Thursday"; break;
case5:
return @"Friday"; break;
case6:
return @"Saturday"; break;
case7:
return @"Sunday"; break;
default:
return @"NO Week"; break;
}
NSLog(@"%@",components);
}
2、将网络数据读取为字符串
-(NSString *)getDataByURL:(NSString *)url {
return [[NSString alloc] initWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]] encoding:NSUTF8StringEncoding];
}
3、读取⺴络图⽚
-(UIImage *)getImageByURL:(NSString *)url {
return [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
}
4、多线程(这种方式,只管建立线程,不管回收线程)
[NSThread detachNewThreadSelector:@selector(scheduleTask) toTarget:self withObject:nil];
-(void)scheduleTask
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pool release];
}
如果有参数,则这么⽤
[NSThread detachNewThreadSelector:@selector(scheduleTask:) toTarget:self withObject:[NSDate date]];
-(void)scheduleTask:(NSDate *)mdate
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pool release]; }
在线程⾥运⾏主线程⾥的⽅法
[self performSelectorOnMainThread:@selector(moveToMain) withObject:nil waitUntilDone:FALSE];
5、⽤户缺省值NSUserDefaults读取:
NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
NSArray *languages = [df objectForKey:@"AppleLanguages"];
NSLog(@"all language is %@",languages);
NSLog(@"index is %@",[languages objectAtIndex:0]);
NSLocale *currentLocale = [NSLocale currentLocale];
NSLog(@"language Code is %@",[currentLocale objectForKey:NSLocaleLanguageCode]);
NSLog(@"Country Code is %@",[currentLocale objectForKey:NSLocaleCountryCode]);
6、view之间转换的动态效果设置
SecondViewController *secondViewController = [[SecondViewController alloc] init];
secondViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;//⽔水平翻转
[self.navigationController presentModalViewController:secondViewController animated:YES];
[secondViewController release];
7、UIScrollView 滑动用法: -(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"正在滑动中。。")
}
//⽤户直接滑动UIScrollView,可以看到滑动条
-(void)scrollViewDidEndDelerating:(UIScrollView *)scrollView {
}
//通过其他控件触发UIScrollView滑动,看不到滑动条
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
}
//UIScrollView 设置滑动不超出本⾝身范围
[scrollView setBounces:NO];
8、iphone的系统目录:
//得到Document:目录
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//得到temp临时目录
NSString *temPath = NSTemporaryDirectory();
//得到目录上的文件地址
NSString *address = [paths stringByAppendingPathComponent:@"1.rtf"];
9、状态栏显⽰示indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
10、app Icon显示数字:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:5];
}
11、sqlite保存地址:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMask, YES);
NSString *thePath = [paths objectAtIndex:0];
NSString *filePath = [thePath stringByAppendingPathComponent:@"kilometer.sqlite"];
NSString *dbPath = [[[NSBundle mainBundle] resourcePathstringByAppendingPathComponent:@"kilometer2.sqlite"];
12、键盘弹出隐藏,textfield变位置
_tspasswordTF = [[UITextField alloc] initWithFrame: CGRectMake(100,
150, 260, 30)];
_tspasswordTF.backgroundColor = [UIColor redColor];
_tspasswordTF.tag = 2;
/*
Use this method to release shared resources, save user data,
_tspasswordTF.delegate = self;
[self.window addSubview: _tspasswordTF];
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[self animateTextField: textField up: YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self animateTextField: textField up: NO];
[textField resignFirstResponder];
return YES;
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up {
const int movementDistance = 80;
// tweak as needed
const float movementDuration = 0.3f;
// tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration];
self.window.frame = CGRectOffset(self.window.frame, 0, movement);
[UIView commitAnimations];
}
13、获取图片的尺⼨
CGImageRef img = [imageView.image CGImage];
NSLog(@"%d",CGImageGetWidth(img));
NSLog(@"%d",CGImageGetHeight(img));
14、AlertView,ActionSheet的cancelButton点击事件:
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex: (NSInteger)buttonIndex; // before animation and hiding view
{
NSLog(@"cancel alertView... buttonindex = %d",buttonIndex); //当⽤用户按下Cancel按钮
if (buttonIndex == [alertView cancelButtonIndex]){
exit(0);
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"%s %d button = %d cancel actionSheet.....",__FUNCTION__,__LINE__, buttonIndex);
//当⽤用户按下Cancel按钮
if (buttonIndex == [actionSheet cancelButtonInd
补充:移动开发 , IOS ,
上一个:IOS dispatch_once
下一个:How to resolve "valid signing identity not found" in provisioning profiles library
- 更多wap疑问解答:
- 新人求助QPainter
- 为什么程序都退出了还可以收到推送?如果大多设备都可以推送那运营商怎么办?
- qt 4.7 sqlserver2000 存储过程调用
- 关于ANDROID4.0.1编译问题!
- Android FrameBuffer读屏幕30秒后mmap失败
- 联通粗定位用java程序如何来请求和接受数据
- 为什么QT运行Android平台的程序时,mouseMoveEvent事件响应的间隔时间很长??????????
- android与PC蓝牙通讯
- 指定大小的label 内容可变,如果内容超出label的宽度,将未能显示的部分显示在另一个label上
- Android调试
- android如何通过wifi连接无线打印机
- 运行程序,release目录下产生一个乱码文件夹
- 分享个某机构最新安卓资料,自己验证了
- service启动不起来,掉不了service connection
- 求助:QT5.0 没有QPrinter吗