- 回调到底是怎么一个概念呢?之前听得多,但是一直对它的解释都是很模糊的。那么今天要总结的就是ios中block的回调应用。(使用block在两个控制器中传值。)
- 先来看效果图
当点击完第二页的按钮时,将单例中的值取出来。
可以看到值已经被修改为ozx - 那么看代码
//
// ViewController.m
// block的转页使用
//
// Created by iiiiiiiii on 15/5/18.
// Copyright (c) 2015年 ozx. All rights reserved.
//
#import "ViewController.h"
#import "LoginViewController.h"
#import "publicValue.h"
@interface ViewController ()
@property (nonatomic,weak) UIButton *my_btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton * b = [UIButton buttonWithType:UIButtonTypeSystem];
b.frame = CGRectMake(100, 100, 100, 100);
// b.titleLabel.text = @"first";
[b setTitle:@"frist" forState:UIControlStateNormal];
b.backgroundColor = [UIColor greenColor];
self.view.backgroundColor = [UIColor whiteColor ];
[publicValue shareValue].isLogin = NO;
[b addTarget:self action:@selector(hehe) forControlEvents:UIControlEventTouchDown];
self.my_btn = b ;
[self.view addSubview:b];
}
- (void)hehe{
LoginViewController *login = [[LoginViewController alloc] initWithResultBlock:^(BOOL isLogin) {
if (isLogin)
{
NSString * heheda = [[publicValue shareValue].userInof objectForKey:@"account"];
[self.my_btn setTitle:heheda forState:UIControlStateNormal];
}
} Animation:YES];
[self presentViewController:login animated:YES completion:^{
}];
}
@end
- 在第一个控制器中,我们随便搞了一个按钮。然后设置点击事件为- (void)hehe;当点击按钮的时候,将会进入到第二个页面。当从第二个页面回来以后block里面的内容,输出单例中account的值。怎么做到要先看完第二页代码
//第二页代码
// LoginViewController.m
// block的转页使用
@implementation LoginViewController
@synthesize isPopAnimation;
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * b = [UIButton buttonWithType:UIButtonTypeSystem];
b.frame = CGRectMake(100, 100, 100, 100);
[b setTitle:@"secend" forState:UIControlStateNormal];
b.backgroundColor = [UIColor greenColor];
self.view.backgroundColor = [UIColor whiteColor ];
[b addTarget:self action:@selector(xixi) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:b];
}
-(void)xixi{
[[publicValue shareValue].userInof setValue:@"ozx" forKey:@"account"];
// 这里是调用一个block,相当于调用了方法,被调用时的block进栈,等待被回调。
// [self getLoginResult:self.view UserAccount:@"hhehe" PassWord:@"hehe" ResultBlock:^(NSDictionary *dicResult) {
//
// if (dicResult) {
[publicValue shareValue].isLogin = YES;
getLoginResultBlock([publicValue shareValue].isLogin);
[self.navigationController popViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:^{
}];
// }
//
// }];
}
这里的block,相当于一个方法,一调用代码块就会被执行,
//-(void)getLoginResult:(UIView *)viewBack UserAccount:(NSString *)strAccount PassWord:(NSString *)strPwd
// ResultBlock:(void(^)(NSDictionary *dicResult))block
//{
// getLoginResultBlockk=nil;
// getLoginResultBlockk=[block copy];
// NSDictionary *dicData = @{@"account":@"12345" };//令字典不为空而已,没什么关系的.
// [[publicValue shareValue].userInof setValue:@"呵呵,改了ozx" forKey:@"account"];
// getLoginResultBlockk(dicData);
//}
-(instancetype)initWithResultBlock:(void(^)(BOOL isLogin))block Animation:(BOOL)isPop
{
self=[super init];
if (self)
{
}
return self;
}
@end
- 第二页还是只有一个按钮,按下就会xixi方法,然后对单例对象中的account进行赋值,值为ozx。并且令isLogin 的值为YES。然后就开始执行getLoginResultBlock([publicValue shareValue].isLogin);这个就是回调函数,执行完这一句以后,就会返回到第一页的block中,执行完以后就会调用dismissViewControllerAnimated:方法回到第一页
- 如果不明白,只要记住,block相当于oc方法。你只要调用它,{}方括号里面的代码就会执行,如
[self getLoginResult:self.view UserAccount:@"hhehe" PassWord:@"hehe" ResultBlock:^(NSDictionary *dicResult) {
if (dicResult) {//这里面的代码需要回调
[publicValue shareValue].isLogin = YES;
getLoginResultBlock([publicValue shareValue].isLogin);
[self.navigationController popViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}];
}
;你如果在调用函数的参数中用到block,那么就要用回调函数来回调执行{}里面的代码。如:
这里面的代码就想调用方法一样被直接调用。
//-(void)getLoginResult:(UIView *)viewBack UserAccount:(NSString *)strAccount PassWord:(NSString *)strPwd
// ResultBlock:(void(^)(NSDictionary *dicResult))block
//{
// getLoginResultBlockk=nil;
// getLoginResultBlockk=[block copy];
// NSDictionary *dicData = @{@"account":@"12345" };//令字典不为空而已,没什么关系的.
// [[publicValue shareValue].userInof setValue:@"呵呵,改了ozx" forKey:@"account"];
// getLoginResultBlockk(dicData);
//}
- 如果你觉得你已经看懂了,不妨试试将第二页的注释打开,再运行一下,看看还明不明白吧。
- 源码在这源代码
- https://github.com/ouzhenxuan/ios-block-used
- 有写得不明白的请留言,一天之内应该可以回答到