解决方案 »
- 新手问一个ipad上显示图片的问题
- iPhone手机平台客户端外包
- 为什么MKMapView显示的数据和原生的系统显示不一致
- iOS开发,deflate算法进行数据压缩
- 如何在MAC系统上发布Unity,并烧入Ipad
- 横竖屏转换以及一些小问题
- NSDecimal 保留2位小数
- iOS 按钮(自定义功能类似单选按钮)处于不同状态时,改变背景色,如何实现。
- 我用afnetworking post 传参数,有个参数不知道怎么处理
- ios socket数据传输中汉字的转码和解码
- ios8.1.2能降级到ios7吗?
- 我使用FMDB来操作数据库,经常会被锁了,而且我使用的单例模式同样发生锁的情况发生,大家能帮我解答一下好吗,
-(void) connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
获取到receivedata后通过delegate 或者block 传回到自己需要的地方
就是不会用delegate回传阿,给点代码提示还行啊
httpComm *http=[[httpComm alloc]init];
http.delegate = self ;
[http sendRequest:url params:dc];-(void) httpFinish:(NSData *)data
{
//获取数据
}-(void) connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data{
if (self.conn != theConnection) return;
[receiveData appendData:data];
NSString *str = [[NSString alloc] initWithData:receiveData encoding:NSUTF8StringEncoding];
NSLog(@"receive data: %@", str);
[delegate httpFinish:receiveData];
}
我把-(void) httpFinish:(NSData *)data
{
//获取数据
NSLog(...);
}
加在controler.m里了
但是nslog并没有发现任何数据的到来阿。
-(void) connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data{
if (self.conn != theConnection) return;
[receiveData appendData:data];
NSString *str = [[NSString alloc] initWithData:receiveData encoding:NSUTF8StringEncoding];
NSLog(@"receive data: %@", str);
<span style="color: #FF0000;"> [delegate httpFinish:receiveData];</span>
}这个仍然是放在httpcomm.m里的是吧
-(void) httpFinish:(NSData *)data
{
//获取数据
}
究竟何时被调用阿,data是传入的参数阿,我想把数据传出去怎么办呢
@interface ViewController ()<httpDelegate>
然后httpComm 实例的时候需要设置delegate
httpComm *http=[[httpComm alloc]init];
http.delegate = self ;
[http sendRequest:url params:dc];然后实现httpDelegate
-(void) httpResponse:(NSHTTPURLResponse *)response
{}
-(void) httpFinish:(NSData *)data
{}
-(void) commError
{}httpcomm.m修改
在 httpcomm 里面合适的地方分别 调用
[delegate httpResponse:httpResponse];
[delegate httpFinish:self.receiveData];
[delegate commError];比如你说"-(void) connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data里可以得到receivedata值"
那么
-(void) connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
{
…………
[delegate httpFinish:data];
}
那么在ViewController.m实现httpDelegate的
-(void) httpFinish:(NSData *)data
{
//这里可以拿到data
}
{
}里头获得的data如何传给 ViewController的viewdidload()??
因为我的sendrequest是在里头实现的,我想接收response。获取data数据,以便进行下面的操作。我现在暂时改用同步get做的,异步情况下,怎么把data如何传给 ViewController的viewdidload()??或者说传到sendrequest的主逻辑代码处??
你可以换一种实现方法,把
httpComm *http=[[httpComm alloc]init];
[http sendRequest:url params:dc];
单独封装出来,放在其他地方实现,等请求完成后也就是
-(void) httpFinish:(NSData *)data
{
ViewController *vc =[ [ViewController alloc] init]
// 然后再给vc设置一个属性,赋值data,这样viewdidload ()里面就可以获取的到服务器的返回数据了
}