当前位置:编程学习 > wap >>

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 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,