预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共36页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

iPhone开发教程之重新构建iPhone聊天应用程序(二)在这一节,可以开始深入项目正题,实现视图控制器。虽然代码都在一个文件中,但是我把它们分成好几个清单,以便解释每一部分时更简单一点。第一部分,清单6,介绍应用程序开始部分和视图控制器的初始化。#import"iOSChatClientViewController.h"@implementationiOSChatClientViewController@synthesizemessageText,sendButton,messageList;-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{if((self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil])){lastId=0;chatParser=NULL;}returnself;}-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{returnYES;}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];}-(void)viewDidUnload{}#import"iOSChatClientViewController.h"@implementationiOSChatClientViewController@synthesizemessageText,sendButton,messageList;-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{if((self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil])){lastId=0;chatParser=NULL;}returnself;}-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{returnYES;}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];}-(void)viewDidUnload{}-(void)dealloc{[superdealloc];}这是标准的iOS代码。代码中有一些对可变的系统事件(比如说内存警告和存储单元分配)的回调。在生产应用程序中,您想要完美地处理这些事件,但是对于这个示例应用程序来说,我不想让事情过于复杂。第一个真正的任务是对messages.php脚本发出GET请求。清单7展示了此任务的代码。-(void)getNewMessages{NSString*url=[NSStringstringWithFormat:@"http://localhost/chat/messages.php?past=%ld&t=%ld",lastId,time(0)];NSMutableURLRequest*request=[[[NSMutableURLRequestalloc]init]autorelease];[requestsetURL:[NSURLURLWithString:url]];[requestsetHTTPMethod:@"GET"];NSURLConnection*conn=[[NSURLConnectionalloc]initWithRequest:requestdelegate:self];if(conn){receivedData=[[NSMutableDatadata]retain];}else{}}-(void)connection:(NSURLConnection*)connectiondidReceiveResponse:(NSURLResponse*)response{[receivedDatasetLength:0];}-(void)connection:(NSURLConnection*)connectiondidReceiveData:(NSData*)data{[receivedDataappendData:data];}-(void)connectionDidFinishLoading:(NSURLConnection*)connection{if(chatParser