最近刚学了几招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中修改的值。
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属性的,这里属于网络请求,不做过多解释。
- (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。因为在+号方法中不能直接给属性赋值。- (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];
}这是今天新get的技能,在这里分享给大家,我是个新手,有上面问题还望能得到大家的指导。谢谢!
block传值以及利用block封装一个网络请求类,布布扣,bubuko.com
原文:http://blog.csdn.net/u013950854/article/details/34938855