iOS

阳历转换成阴历的源代码

-(NSString )LunarForSolar:(NSDate )solarDate

{

//天干名称

NSArray *cTianGan = [NSArrayarrayWithObjects:@”甲”,@”乙”,@”丙”,@”丁”,@”戊”,@”己”,@”庚”,@”辛”,@”壬”,@”癸”, nil];

//地支名称

NSArray *cDiZhi = [NSArrayarrayWithObjects:@”子”,@”丑”,@”寅”,@”卯”,@”辰”,@”巳”,@”午”,@”未”,@”申”,@”酉”,@”戌”,@”亥”,nil];

//属相名称

NSArray *cShuXiang = [NSArrayarrayWithObjects:@”鼠”,@”牛”,@”虎”,@”兔”,@”龙”,@”蛇”,@”马”,@”羊”,@”猴”,@”鸡”,@”狗”,@”猪”,nil];

ios设置时区转换

有的时候为了在系统中统一时间,需要在服务器和客户端统一交换的时间时区,比如都用GMT。
iPhone上转换的代码如下:

NSDate now = [NSDate date];
NSLog(@”%@”,[now description]);
NSTimeZone
defaultTimeZone = [NSTimeZone defaultTimeZone];
NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@”GMT”];
[NSTimeZone setDefaultTimeZone:tzGMT];
NSLog(@”%@”,[now description]); //已经是 GMT表示了
[NSTimeZone setDefaultTimeZone:defaultTimeZone]; // 设置会用户默认的

或者:

[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@”CMT”]];

定位源代码位置的系统宏

调试代码若出现错误,为了快速定位错误代码位置,可用一些系统定义的宏,如:

NSLog(@”错误发生位置:文件名:%s,函数名:%s,代码行数:%d”,FILE,FUNCTION,LINE);

操作图片

//将图片保存到本地

+ (void)SaveImageToLocal:(UIImage*)image Keys:(NSString*)key {
    NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
    //[preferences persistentDomainForName:LocalPath];
    [preferences setObject:UIImagePNGRepresentation(image) forKey:key];
}

//本地是否有相关图片
+ (BOOL)LocalHaveImage:(NSString*)key {
    NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
    //[preferences persistentDomainForName:LocalPath];
    NSData* imageData = [preferences objectForKey:key];
    if (imageData) {
        return YES;
    }
    return NO;
}

//从本地获取图片
+ (UIImage*)GetImageFromLocal:(NSString*)key {
    NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
    //[preferences persistentDomainForName:LocalPath];
    NSData* imageData = [preferences objectForKey:key];
    UIImage* image;
    if (imageData) {
        image = [UIImage imageWithData:imageData];
    }
    else {
        NSLog(@"未从本地获得图片");
    }
    return image;
}

UIWebView打开doc、pdf文件

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 55, 320, 300)];
webView.delegate = self;
webView.multipleTouchEnabled = YES;
webView.scalesPageToFit = YES;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docPath = [documentsDirectory stringByAppendingString:@"/doc2003_1.doc"];    NSLog(@"#######%@",docPath);

NSURL *url = [NSURL fileURLWithPath:docPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

[self.view addSubview:webView];
[webView release];

加载自定义Cell

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @”Cell”;

    CustomCell cell = (CustomCell )[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
    cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@”CustomCell” owner:self options:nil];

    for (id obj in nib) {
    if ([obj isKindOfClass:[CustomCell class]]) {
    cell = (CustomCell *) obj;
    }
    }

    }
    return cell;
    }

读取保存plist文件

  • (NSString )dataFilePath{
    NSArray
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@”config.plist”];
    }

    +(NSMutableDictionary )loadFromFile {
    NSString
    error = nil;
    NSPropertyListFormat format;
    NSMutableDictionary dict = nil;
    NSString
    filePath = [self dataFilePath];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    filePath = [[NSBundle mainBundle] pathForResource:@”config” ofType:@”plist”];
    }
    NSData plistXML = [[NSFileManager defaultManager] contentsAtPath:filePath];
    dict = (NSMutableDictionary
    )[NSPropertyListSerialization propertyListFromData:plistXML
    mutabilityOption:NSPropertyListMutableContainersAndLeaves
    format:&format
    errorDescription:&error];
    return dict;
    }

    +(BOOL)saveToFile:(NSMutableDictionary )withData {
    NSString
    error = nil;
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:withData format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
    if(plistData) {
    return [plistData writeToFile:[self dataFilePath] atomically:YES];
    } else {
    return FALSE;
    }
    }

view添加点击方法

UITapGestureRecognizer singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSameThing1)];
UITapGestureRecognizer
trippleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSameThing2)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSameThing3)];
[doubleTap setNumberOfTapsRequired:2];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:2];
[trippleTap setNumberOfTapsRequired:1];
[trippleTap setNumberOfTouchesRequired:3];
[table addGestureRecognizer:doubleTap];
[table addGestureRecognizer:singleTap];
[table addGestureRecognizer:trippleTap];

[singleTap release];
[doubleTap release];
[trippleTap release];

iOS开发常用的开源类库和一些示例

MBProgressHUD_——进展指示符库">MBProgressHUD ——进展指示符库

苹果的应用程序一般都会用一种优雅的,半透明的进度显示效果,不过这个API是不公开的,因此你要是用了,很可能被清除出AppStore。而 MBProgressHUD提供了一个替代方案,而且在用户角度上,实现的效果根本看不出和官方程序有什么差别。同时还提供了其他附加功能,比如虚拟进展 指示符,以及完成提示信息。整合到项目里也很容易,这里不细谈了。