ASIHTTPRequest:异步请求之后 用代理传值问题 代理 块

仅供参考,自己也是新手,有说的不对的欢迎大家指教

 转载注明出处http://blog.csdn.net/u011111270/article/details/24626335

本机环境   xcode5.1  OS X 10.9.2


ASIHTTPRequest:异步请求的使用大家可以参考下面几篇博文

http://www.th7.cn/Program/IOS/2012/07/15/84251.shtml
http://itjoy.org/?p=377


有关ASIHTTPRequest的使用碰到的问题也可以参考cocoachina 

http://www.cocoachina.com/ask/questions/index/tagged/ASIHTTPRequest?page=1&tab=tagged


//主要就是使用代理和block 但是自己在使用的时候2 个viewControll 内存上有点问题 总是说bad access 然后代理的位置的内存就是空的。。就使用块了。。待续


代理这个问题解决了 在2个viewControll中 要把你要的中间类设置为成员变量(自己摸索真的蛮辛苦) 就是像这样

@interface adoubiViewController ()<secondViewContollerDelegate>
{
    //ASIHTTPRequest *request;
    secondViewController *second;
}

把你要的中间类(这里是  secondViewController)放在成员变量里 

如果这样写 

//second 就会被释放,然后second.delegate指向的地址为空,就会报错
    secondViewController *second=[[secondViewController alloc]init];
    
    [second setDelegate:self];
    [self.view addSubview:second.view];

就会出错提示  exc_bad_access 原因就在于下面的delegate [0] 





secondViewController 中就比较简单 直接响应ASIHTTPRequestDelegate


附上全部的代码

secondViewController.h

#import <UIKit/UIKit.h>
//通过protocol新建一个delegate
@protocol secondViewContollerDelegate
@optional
- (void)delegateTest:(NSString *)request;
@end

@interface secondViewController : UIViewController
{
//    id<secondViewContollerDelegate> delegate;
}

//委托者secondViewController 声明一个delegate
@property (weak,nonatomic)id<secondViewContollerDelegate> delegate;
@end

secondViewController.m

#import "secondViewController.h"
#import "ASIHTTPRequest.h"

@interface secondViewController ()<ASIHTTPRequestDelegate>

@end

@implementation secondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //从地址请求数据
    NSURL *url=[NSURL URLWithString:@"http://localhost/ts.php"];
    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];
    //设置ASIHTTPRequestDelegate
    [request setDelegate:self];
    //异步启用
    [request startAsynchronous];
}
#pragma mark ASIHTTPRequest delegate
//------------当异步请求完成是调用requestFinished
-(void)requestFinished:(ASIHTTPRequest *)request{
    
    [self.delegate delegateTest:[request responseString]];//委托者 也就是secondViewController 调用代理方法
    NSLog(@"request finished");
}
//------------当异步请求出错时调用requestFailed
-(void)requestFailed:(ASIHTTPRequest *)request{
    [self.delegate delegateTest:@"error"];
    NSLog(@"request faild");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


adoubiViewController.h 文件没改动

adoubiViewController.m

#import "adoubiViewController.h"
#import "secondViewController.h"
//#import "testDelegate.h"

@interface adoubiViewController ()<secondViewContollerDelegate>
{
    //ASIHTTPRequest *request;
    secondViewController *second;
}

@end

@implementation adoubiViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //second 就会被释放,然后second.delegate指向的地址为空,就会报错
    //secondViewController *second=[[secondViewController alloc]init]; //这句在这里使用会出错
    //所以要这样写
    second=[[secondViewController alloc]init];
    [second setDelegate:self];
    [self.view addSubview:second.view];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)delegateTest:(NSString *)request{
    NSLog(@"this is ASIHTTPRequest %@",request);
}
@end

玩了一个月的ios 水很深。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值