block传值以及利用block封装一个网络请求类

来源:互联网 发布:淘宝行业数据大揭秘 编辑:程序博客网 时间:2024/05/17 00:17

1.block在俩个UIViewController间传值

最近刚学了几招block 的高级用法,其实就是利用block语法在俩个UIViewController之间传值,在这里分享给初学者,同时也方便我自己理解。我们知道UINavigationController类管理UIViewController的时候,利用的是“栈”的思想,在这里不做过多解释,切入正题,假设我们现在有俩个UIViewController,viewC1和viewC2,viewC1比viewC2先进入到UINavigationController里,即viewC2是由viewC1push出来的,这里的传值是指将viewC2的值传到viewC1里面。例如通讯录的修改,微信里修改信息等。

不多说,直接上例子:

一.首先在viewC2的头文件中的代码: 

//1 。重定义一个block类型typedef void(^BL)(UIColor *color);typedef void(^stringBL)(NSString *string);@interface SecondViewController : UIViewController//定义一个block属性 一定要使用copy特性  原因:@property (nonatomic, copy) BL block;@property (nonatomic, copy) stringBL string;- (void)valueBlock:(BL)block;- (void)valueStringBlock:(stringBL)block;
提示:

重定义block在Xcode中有快捷实现方式,直接在@interfacexxx 上面一行打typedef会有提示: 直接回车,第一个参数是返回值类型,第二个为重定义的block名称,第三个为要传参数类型和参数名;然后需要定义重定义block类型的属性,并且实现参数为该重定义类型block的方法。

二.viewC2的.m文件中代码:

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];    button.frame = CGRectMake(0, 70, 320, 40);    [self.view addSubview:button];    [button setTitle:@"return 1 page" forState:UIControlStateNormal];    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];}- (void)buttonAction:(UIButton *)button{//    2.在合适的地方执行block代码    self.block([UIColor redColor]);    self.string(@"asdasdasd");    [self.navigationController popToRootViewControllerAnimated:YES];}- (void)valueBlock:(BL)block{    self.block = block;}- (void)valueStringBlock:(stringBL)block{    self.string = block;}
解释:实现头文件中定义的方法,方法内部写将参数赋值给重定义block的属性,然后在合适的地方执行block代码,block里面的参数即你要传的值。

三.在viewC1中push第二个页面的时候调用 viewC2的方法,直接回车,block重定义中的参数名里存的即是要在viewC1中修改的值。


2.利用block封装网络请求类。

1.创建网络请求类NetRequest,头文件如下
typedef void(^BLOCK)(id result);@interface NetRequest : NSObject@property (nonatomic, copy) BLOCK bl;- (void)requestNetWithUrl:(NSString *)urlStr BodyOfRequestForString:(NSString *)bodyStr block:(BLOCK)bl;+ (void)PostWithUrl:(NSString *)urlStr BodyOfRequestForString:(NSString *)bodyStr block:(BLOCK)bl;@end
解释:该处利用的是异步post传值,头文件写法基本上跟block传值一样,我这里写了一个+号方法,为的是能在使用NetRequest类的时候更加方便。可以看到这里的方法有三个参数,分别是url的地址,以及设置request的HttpBody属性的,这里属于网络请求,不做过多解释。
2.下面看.m文件:实现俩个方法
- (void)requestNetWithUrl:(NSString *)urlStr BodyOfRequestForString:(NSString *)bodyStr block:(BLOCK)bl{    self.bl = bl;    NSURL *url = [NSURL URLWithString:urlStr];    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];    request.HTTPMethod = @"post";    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];    request.HTTPBody = bodyData;    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {       NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options :NSJSONReadingMutableContainers error:nil];        NSLog(@"%d", [[dic objectForKey:@"news"] count]);        self.bl(dic);        }];    }+ (void)PostWithUrl:(NSString *)urlStr BodyOfRequestForString:(NSString *)bodyStr block:(BLOCK)bl{    NetRequest *netRequest = [[NetRequest alloc] init];    [netRequest requestNetWithUrl:urlStr BodyOfRequestForString:bodyStr block:bl];}
解释:-方法中首先要设置属性block=参数block,再将要传的值添加到block方法的参数中。+号方法是利用-号方法将参数block传给属性block。因为在+号方法中不能直接给属性赋值。

3.如何使用:将数据传输到ViewController上
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization        self.array = [NSMutableArray array];        self.title = @"新闻";        NSString *urlStr = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";        NSString *bodyStr = @"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";        [NetRequest PostWithUrl:urlStr BodyOfRequestForString:bodyStr block:^(id result) {            self.array = [result objectForKey:@"news"];            [self.tableView reloadData];        }];    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];    [self.view addSubview:self.tableView];    self.tableView.dataSource = self;    self.tableView.delegate = self;    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];    }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    NSString *str = @"reuse";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];    cell.textLabel.numberOfLines = 2;    cell.textLabel.text = [[self.array objectAtIndex:indexPath.row] objectForKey:@"title"];    return cell;}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    WebViewController *webVC = [[WebViewController alloc] init];    webVC.webUrl = [[self.array objectAtIndex:indexPath.row] objectForKey:@"newsUrl"];    [self.navigationController pushViewController:webVC animated:YES];    [webVC release];}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [self.array count];}


下面来看看为什么我们要用这种传值方式:因为我要把当前获得的数据传输给一个UIViewController,而这个UIViewController上面有UITableView,需要刷新,而我们获取网络数据的时候使用的是利用block封装的一个获取NSData的方法,该方法会在数据加载完后才会执行块语句,而且网络会有延迟,我们的UI界面会优先于数据显示在手机上,所以当数据请求成功后,我们需要刷新页面,即调用UITableView的reloadData方法,所以才使用这种策略。

这是今天新get的技能,在这里分享给大家,我是个新手 我的QQ 263506069,上面有问题还望能得到大家的指导。谢谢!

0 0
原创粉丝点击