block格式_typedef: 返回值(^block名字)(参数)
(1)定义block
typedef void(^myBlcok)(NSArray *array);
(2)申明block属性
@property (strong, nonatomic) myBlcok block;
用block存储值
NSArray *array=[[NSArray alloc]initWithObjects:_userNameText.text,_passWordText.text ,nil];
if (self.block)
{
self.block(array);
}
拿到block的值
VC.block=^(NSArray *array){
_userNameText.text=array[0];
_passWordText.text=array[1];
};
闭包_typealias
1.首先,创建闭包类型
typealias SwiftBlock = (phone:String?, password:String?) -> Void
2.定义闭包函数变量
private var block:SwiftBlock?
3.声明含有闭包变量的函数
func setTitle(title: String, account: SwiftBlock){
navigationItem.title = title
self.account = account
}
4.触发闭包函数
if let nameAndPassword = account {
nameAndPassword(phone: phone, password: password)
}
5.闭包回调
VC.setTitle("登录界面") { (phone, password) in
print("phone:\(phone)password:\(password)")
}