iPhone学习笔记几个实例开发操作

移动开发 iOS
本文介绍的是iphone应用中几个小结实例开发,很简单的实现了他们的功能,先来看详细内容。

iPhone学习笔记几个实例开发操作是本文要介绍的内容,主要有ios震动NSUserDefaults用法、如何读取文件等内容,来看本文内容。

IOS震动

震动是声音的一种,用如下方式:

  1. #import <AudioToolbox/AudioToolbox.h>   
  2. #import <UIKit/UIKit.h>   
  3. - (void)vibrate   
  4. {   
  5.     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);   
  6. }  

调用到震动器震动。

iPhone中定时器NSTimer使用方法

  1. - (void) applicationDidFinishLaunching: {  
  2. NSTimer *_timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES] retain];  
  3. }  
  4. // call function when time is up (in this exemple :1 second. to change it, change scheduledTimerWithTimeInterval: 1 to value in second)  
  5. - (void)onTimer  
  6. {  
  7. // schedule code..  

NSUserDefaults用法

  1. -(void)saveToUserDefaults:(NSString*)myString   
  2. {   
  3. NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];   
  4. if (standardUserDefaults)        
  5. {   
  6. [standardUserDefaults setObject:myString forKey:@"Prefs"];   
  7.        [standardUserDefaults synchronize];  
  8.      }   
  9. }     
  10. -(NSString*)retrieveFromUserDefaults   
  11. {   
  12. NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];    
  13. NSString *val = nil;   
  14. if (standardUserDefaults)         
  15. val = [standardUserDefaults objectForKey:@"Prefs"];   
  16. return val;   

iPhone中如何从Application Bundle中读取文件

首先必须将文件加入Xcode工程的Resources目录。然后可以如下访问文件,假设文件为MyFile.txt:

  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];       
  2.  
  3. NSData *myData = [NSData dataWithContentsOfFile:filePath];       
  4.  if (myData) {       
  5.     // do something useful       
  6.  }  

一段将help文本文件读入UIWebView的完整示例:

  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];       
  2.  NSData *htmlData = [NSData dataWithContentsOfFile:filePath];       
  3.  if (htmlData) {       
  4.      [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];       
  5.  }  

如果想将文件读入字符串,则可以用UITextView显示,例如:

  1. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];    
  2.  if (filePath) {    
  3.     NSString *myText = [NSString stringWithContentsOfFile:filePath];    
  4.      if (myText) {    
  5.          textView.textmyText;    
  6.     }    
  7.  }  

小结:iPhone学习笔记几个实例开发操作的内容介绍完了,希望通过本文的学习对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-15 10:06:22

iPhone开发nib 文件

2011-08-09 17:29:29

iPhone文件屏幕

2011-08-08 14:57:46

iPhone Autoreleas Property

2011-07-07 16:42:38

iPhone Sqlite3 数据库

2011-08-03 16:01:24

iPhone应用开发 自动登陆

2011-07-25 18:02:51

iPhone LibFetion 移植

2011-08-12 09:48:24

iPhoneCoreLocatio定位

2011-08-19 11:10:31

iPhone应用

2011-07-26 11:13:15

iPhone PXL

2011-08-08 16:56:44

iPhone 字符处理 视图

2011-07-18 13:37:53

2011-08-16 18:13:42

IPhone开发UIView动画

2011-08-19 09:49:03

iPhone开发Three20 NetTTRequestLo

2011-07-18 17:52:47

Linux iPhone

2011-07-27 17:07:06

iPhone 游戏 Cocos2d

2011-08-19 10:13:05

iPhone开发

2011-07-28 10:11:54

iPhone开发 备忘

2011-07-27 11:19:33

iPhone UITableVie

2011-08-22 13:46:15

iPhone开发GameKit 蓝牙

2011-07-06 16:25:10

iPhone 程序 调用
点赞
收藏

51CTO技术栈公众号