一些常用操作

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

[nowDate release]; NSCalendar用法: -(NSString *) getWeek:(NSDate *) d { NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit; NSDateComponents *components = [calendar components:units fromDate:d]; [calendar release]; switch ([components weekday]) { case 2: return @"Monday"; break; case 3: return @"Tuesday"; break; case 4: return @"Wednesday"; break; case 5: return @"Thursday"; break; case 6: return @"Friday"; break; case 7: return @"Saturday"; break; case 1: return @"Sunday"; break;
default: return @"No Week"; break; } // 用components,我们可以读取其他更多的数据。 } 4. 用Get方式读取网络数据: 将网络数读取为字符串 - (NSString *) getDataByURL:(NSString *) url { return [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncodin g]]] encoding:NSUTF8StringEncoding]; } //读取网络图片 - (UIImage *) getImageByURL:(NSString *) url { return [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncodin g]]]]; } 多线程 [NSThread detachNewThreadSelector:@selector(scheduleTask) toTarget:self withObject:nil];
//得到用户缺省值 NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; //在缺省值中找到AppleLanguages, 返回值是一个数组 NSArray* languages = [defs objectForKey:@"AppleLanguages"]; NSLog(@"all language语言 is %@", languages); //在得到的数组中的第一个项就是用户的首选语言了 NSLog(@"首选语言 is %@",[languages objectAtIndex:0]); //get the language & country code NSLocale *currentLocale = [NSLocale currentLocale]; NSLog(@"Language Code is %@", [currentLocale objectForKey:NSLocaleLanguageCode]); NSLog(@"Country Code is %@", [currentLocale objectForKey:NSLocaleCountryCode 8. View之间切换的动态效果设置:
stringFromDate:nowDate];
objectAtIndex:i],locationTime); // //NSLog(@"timezone name is:%@",[array
objectAtIndex:i]); // } //快速枚举法 for(NSString *timeZoneName in array){ [formatter setTimeZone:[NSTimeZone timeZoneWithName:timeZoneName]]; NSLog(@"%@,%@",timeZoneName,[formatter stringFromDate:nowDate]); } [formatter release];
SettingsController *settings = [[SettingsController alloc] initWithNibName:@"SettingsView" bundle:nil]; settings.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; //水平翻转 [self presentModalViewController:settings animated:YES]; [settings release]; 9.NSScrollView 滑动用法:
-(void) scheduleTask:(NSDate *) mdate { //create a pool NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//release the pool; [pool release]; } //注意selector里有冒号。 //在线程里运行主线程里的方法 [self performSelectorOnMainThread:@selector(moveToMain) withObject:nil waitUntilDone:FALSE]; 6. 定时器NSTimer用法: 代码 // 一个可以自动关闭的Alert窗口
setDateFormat:@"yyyy/MM/dd HH:m源自文库:ss"];
根据时区名字获取当前时间,如果该时区不存在,默认获取系统当前
timeZoneWithName:@"Europe/Andorra"]; // [formatter setTimeZone:timeZone]; //获取所有的时区名字 NSArray *array = [NSTimeZone knownTimeZoneNames]; // NSLog(@"array:%@",array); //for循环 // // // for(int i=0;i<[array count];i++) { NSTimeZone* timeZone = [NSTimeZone
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView
alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarg e];
// Adjust the indicator so it is up a few pixels from the bottom of the alert indicator.center = CGPointMake(alert.bounds.size.width/2, alert.bounds.size.height-40.0); [indicator startAnimating]; [alert insertSubview:indicator atIndex:0]; [indicator release];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[@"一个可以自动关闭的 Alert窗口" delegate:nil cancelButtonTitle:nil //NSLocalizedString(@"OK", @"OK") //取消任何按钮
各地时区获取: NSDate *nowDate = [NSDate new]; NSDateFormatter *formatter [formatter // 时区的时间 // NSTimeZone* timeZone = [NSTimeZone = [[NSDateFormatter alloc] init];
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(dismissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert, @"alert", @"testing ", @"key" ,nil] //如果不用传递参 数,那么可以将此项设置为nil. repeats:NO];
-(void) scheduleTask { //create a pool NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//release the pool; [pool release]; }
//如果有参数,则这么使用: [NSThread detachNewThreadSelector:@selector(scheduleTask:) toTarget:self withObject:[NSDate date]];
UIAlertView *alert = [[timer userInfo] objectForKey:@"alert"]; [alert dismissWithClickedButtonIndex:0 animated:YES];
}
定时器停止使用: [timer invalidate]; timer = nil; 7. 用户缺省值NSUserDefaults读取:
-(void) scrollViewDidScroll:(UIScrollView *)scrollView{ NSLog(@"正在滑动中...");
NSLog(@"release alert"); [alert release];
-(void) dismissAlert:(NSTimer *)timer{
NSLog(@"release timer"); NSLog([[timer userInfo] objectForKey:@"key"]);
otherButtonTitles:nil]; //[alert setBounds:CGRectMake (alert.bounds.origin.x, alert.bounds.origin.y, alert.bounds.size.width, alert.bounds.size.height+30.0)]; [alert show];
timeZoneWithName:[array objectAtIndex:i]]; // // // [formatter setTimeZone:timeZone]; NSString *locationTime = [formatter NSLog(@"时区名字:%@ : 时区当前时间: %@",[array
相关文档
最新文档