iPhone3开发基础教程中部分有用代码片段(1)
??????????????????????????????????? repeats:NO];??
??? NSLog(@"release alert");??
??? [alert release];??
-(void) dismissAlert:(NSTimer *)timer{??
??????
??? NSLog(@"release timer");??
??? NSLog([[timer userInfo]? objectForKey:@"key"]);??
??????
??? UIAlertView *alert = [[timer userInfo]? objectForKey:@"alert"];??
??? [alert dismissWithClickedButtonIndex:0 animated:YES];??
?????
}?
?// 一个可以自动关闭的Alert窗口
??? UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
??????????????????????????????????????????????????? message:[@"一个可以自动关闭的Alert窗口"
?????????????????????????????????????????????????? delegate:nil
????????????????????????????????????????? cancelButtonTitle:nil //NSLocalizedString(@"OK", @"OK")?? //取消任何按钮
????????????????????????????????????????? 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];
???
??? UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
???
??? // 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];
???
??? [NSTimer scheduledTimerWithTimeInterval:3.0f
???????????????????????????????????? target:self
?????????????????????????????????? selector:@selector(dismissAlert:)
?????????????????????????????????? userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert, @"alert", @"testing ", @"key" ,nil]? //如果不用传递参数,那么可以将此项设置为nil.
??????????????????????????????????? repeats:NO];
??? NSLog(@"release alert");
??? [alert release];
-(void) dismissAlert:(NSTimer *)timer{
???
??? NSLog(@"release timer");
??? NSLog([[timer userInfo]? objectForKey:@"key"]);
???
??? UIAlertView *alert = [[timer userInfo]? objectForKey:@"alert"];
??? [alert dismissWithClickedButtonIndex:0 animated:YES];
??
}
?
?7. 用户缺省值NSUserDefaults读取:
view plaincopy to clipboardprint?
//得到用户缺省值??
??? 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]);????
//得到用户缺省值
??? 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之间切换的动态效果设置:
view plaincopy to clipboardprint?
SettingsController *settings = [[SettingsController alloc]initWithNibName:@"SettingsView" bundle:nil];??
settings.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;? //水平翻转??
[self presentModalViewController:settings animated:YES];??
[settings release];?
??? SettingsController *settings = [[SettingsController alloc]initWithNibName:@"SettingsView" bundle:nil];
??? settings.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;? //水平翻转
??? [self presentModalViewController:settings animated:YES];
??? [settings release];
9.NSScrollView 滑动用法:
view plaincopy to clipboardprint?
-(void) scrollViewDidScroll:(UIScrollView *)scrollView{??
??? NSLog(@"正在滑动中...");??
}??
//用户直接滑动NSScrollView,可以看到滑动条??
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {??
}??
// 通过其他控件触发NSScrollView滑动,看不到滑动条??
- (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {??
}?
-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
??? NSLog(@"正在滑动中...");
}
//用户直接滑动NSScrollView,可以看到滑动条
-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
}
// 通过其他控件触发NSScrollView滑动,看不到滑动条
- (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
}
??? 10. 读取全局的Delegate:
view plaincopy to clipboardprint?
KiloNetAppDelegate *appdelegate = (KiloNetAppDelegate *)[[UIApplication sharedApplication] delegate];?
KiloNetAppDelegate *appdelegate = (KiloNetAppDelegate *)[[UIApplication sharedApplication] delegate];
?11.键盘处理系列
view plaincopy to clipboardprint?
//set the UIKeyboard to switch to a different text field when you press return??
//switch textField to the name of your textfield??
[textField becomeFirstResponder];?
//set the UIKeyboard to switch to a different text field when you press return
//switch textField to the name of your textfield
[textField becomeFirstResponder];
12. 半透明层的实现:
view plaincopy to clipboardprint?
+(void)showWaiting:(UIView *)parent {??
??? int width = 32, height = 32;??
??????
??? CGRect frame = [parent frame]; //[[UIScreen mainScreen] applicationFrame];??
??? int x = frame.size.width;??
??? int y = frame.size.height;??
??????
??? frame = CGRectMake((x - width) / 2, (y - height) / 2, width, height);??
??? UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];??
??? [progressInd startAnimating];??
??? progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;??
??????
??????
//??? frame = CGRectMake((x - 140)/2, (y - height) / 2 + height, 140, 30);??
//??? UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];??
//??? waitingLable.text = @"Proccesing...";??
//??? waitingLable.textColor = [UIColor whiteColor];??
//??? waitingLable.font = [UIFont systemFontOfSize:15];??
//??? waitingLable.backgroundColor = [UIColor clearColor];??
??????
??? frame = [parent frame];??
??? UIView *theView = [[UIView alloc] initWithFrame:frame];??
??? theView.backgroundColor = [UIColor blackColor];??
??? theView.alpha = 0.8;??
??????
??? [theView addSubview:progressInd];??
//? [theView addSubview:waitingLable];??
??????
??? [progressInd release];??
//??? [waitingLable release];??
??????
??? [theView setTag:9999];??
??? [parent addSubview:theView];??
??? [theView release];??
}??
+(void)hideWaiting:(UIView *)parent {??
??? [[parent viewWithTag:9999] removeFromSuperview];??
}?
+(void)showWaiting:(UIView *)parent {
??? int width = 32, height = 32;
???
??? CGRect frame = [parent frame]; //[[UIScreen mainScreen] applicationFrame];
??? int x = frame.size.width;
??? int y = frame.size.height;
???
??? frame = CGRectMake((x - width) / 2, (y - height) / 2, width, height);
??? UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];
??? [progressInd startAnimating];
??? progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
???
???
//??? frame = CGRectMake((x - 140)/2, (y - height) / 2 + height, 140, 30);
//??? UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
//??? waitingLable.text = @"Proccesing...";
//??? waitingLable.textColor = [UIColor whiteColor];
//??? waitingLable.font = [UIFont systemFontOfSize:15];
//??? waitingLable.backgroundColor = [UIColor clearColor];
???
??? frame = [parent frame];
??? UIView *theView = [[UIView alloc] initWithFrame:frame];
??? theView.backgroundColor = [UIColor blackColor];
??? theView.alpha = 0.8;
???
??? [theView addSubview:progressInd];
//? [theView addSubview:waitingLable];
???
??? [progressInd release];
//??? [waitingLable release];
???
??? [theView setTag:9999];
??? [parent addSubview:theView];
??? [theView release];
}
+(void)hideWaiting:(UIView *)parent {
??? [[parent viewWithTag:9999] removeFromSuperview];
}
?13. 设置View的圆角:
view plaincopy to clipboardprint?
?// 首先应用? #import <QuartzCore/QuartzCore.h>??
view.layer.cornerRadius = 10;??
view.layer.masksToBounds = YES;?
?// 首先应用? #import <QuartzCore/QuartzCore.h>
view.layer.cornerRadius = 10;
view.layer.masksToBounds = YES;
14.随机数:
view plaincopy to clipboardprint?
srandom(time(NULL)); //随机数种子??
id d = random(); // 随机数?
srandom(time(NULL)); //随机数种子
id d = random(); // 随机数
15.视频播放:
view plaincopy to clipboardprint?
MPMoviePlayerController *moviePlayer;??
?? moviePlayer = [[MPMoviePlayerController alloc]??
????????????????? initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"]]];??
?? //初始化视频播放器对象,并传入被播放文件的地址??
?? moviePlayer.movieControlMode = MPMovieControlModeDefault;??
?? [moviePlayer play];??
?? //此处有内存溢出?
?MPMoviePlayerController *moviePlayer;
??? moviePlayer = [[MPMoviePlayerController alloc]
?????????????????? initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"]]];
??? //初始化视频播放器对象,并传入被播放文件的地址
??? moviePlayer.movieControlMode = MPMovieControlModeDefault;
??? [moviePlayer play];
??? //此处有内存溢出
16. 3.? 启动界面显示:
iPhone软件启动后的第一屏图片是非常重要的往往就是loading载入中的意思。设置它说来也简单,但是却无比重要
只需要在resource里面将你希望设置的图片更名为Default.png,这个图片就可以成为iPhone载入的缺省图片
17.iPhone的系统目录:
view plaincopy to clipboardprint?
//得到Document目录:??
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);??
NSString *documentsDirectory = [paths objectAtIndex:0];??
//得到temp临时目录:??
NSString *tempPath = NSTemporaryDirectory();??
//得到目录上的文件地址:??
NSString *文件地址 = [目录地址 stringByAppendingPathComponent:@"文件名.扩展名"];?
//得到Document目录:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//得到temp临时目录:
NSString *tempPath = NSTemporaryDirectory();
//得到目录上的文件地址:
NSString *文件地址 = [目录地址 stringByAppendingPathComponent:@"文件名.扩展名"];
18.状态栏显示Indicator:
view plaincopy to clipboardprint?
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;??
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
?
?
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ipromiseu/archive/2010/08/22/5830398.aspx
?