先來看看Object-c的block和Swift的閉包的對比
<<<下面主要以兩者使用的方法做對比:
Object-c申明一個在函數中使用Block且不帶參數和沒返回值的block
//:在函數中使用Block不帶參數和沒返回值的block
-(void)GZWork:(NSString *)name withComplated:(void(^)())complated {
NSLog(@"----函數中打印-%@",name);
if (complated) {
complated();
}
}
// ********調用函數
[self GZWork:@"ZZ" withComplated:^{
NSLog(@"----Block--打印");
}];
Object-C 聲明帶參數和有返回值的Block在函數名中
// ***2:聲明帶參數和有返回值的Block在函數名中
-(void) GZWork:(NSString *)name withComplatedRetunStr:(NSString *(^)(NSString * names,NSString * school))complated {
NSLog(@"----函數中打印帶參數有返回值-%@",name);
if (complated) {
complated(name,@"軍事博物館");
}
}
// ********調用
[self GZWork:@"中國" withComplatedRetunStr:^NSString *(NSString *names,NSString * school) {
NSLog(@"----Block函數中打印帶參數有返回值-%@-----%@",names,school);
return names;
}];
Swift 聲明不帶參數和沒返回值的閉包
// 聲明不帶參數和沒返回值的閉包
func HttpTools(names: Int ,complated:() -> ()) -> Int {
let resInt = names + 10
print("1:先執行函數")
complated()
return resInt
}
// ********調用
HttpTools(15) {
print("2:在執行Block了")
}
Swift 聲明帶參數和有返回值的閉包在函數名中
// 聲明帶參數和有返回值的閉包在函數名中
func ajaxTools(name:String ,complated:(runStr: String,isStop:Bool) -> String) -> String {
let resStr = name + "覆水難收"
complated(runStr: resStr, isStop: true)
return resStr + " - 內部函數返回"
}
// ********調用
let ajaxResult = ajaxTools("洲洲哥") { (runStr, isStop) -> String in
print("-----\(runStr)")
return ""
}
OC子頁面傳值給父頁面
在子頁面中聲明一個Block
typedef void(^changUserName)(NSString * userNames);
把Block申明成屬性
@property (nonatomic, copy) changUserName changText;
// 還可把set方法拋出來(或者使用實例方法調用)
-(void)setChangText:(changUserName)changText;
點擊返回按鈕的回調方法我們要這樣寫
-(void)playVideoBack {
if (self.changText) {
self.changText(self.inputFiled.text);
}
[self.navigationController popViewControllerAnimated:YES];
}
在跳轉按鈕的方法里我們這樣寫(兩種方法,對不兩種不同屬性哦)
-(void)ButtonClick {
SecondViewController * sec = [[SecondViewController alloc] init];
/**防止循環引用*/
__weak typeof(self) WeakSelf = self;
// 第一種寫法
// sec.changText = ^(NSString * textStr) {
// WeakSelf.userNames.text = textStr;
// [WeakSelf AFNetWork:@"歷史遺留痕跡" withComplated:^{
// NSLog(@"----block---弱引用");
// }];
// };
// 第二種寫法
[sec setChangText:^(NSString *userNames) {
WeakSelf.userNames.text = userNames;
[WeakSelf AFNetWork:@"歷史遺留痕跡" withComplated:^{
NSLog(@"----block---弱引用");
}];
}];
[self.navigationController pushViewController:sec animated:YES];
}
Swift里子頁面給父頁面傳值
在子頁面文件中 申明一個閉包
typealias changUserName = (String) ->()
把閉包申明成屬性
var changText: changUserName?
// 或者使用實例方法調用(方法名字不固定,但參數是必須的)
func setMyChangeName(tempClose: changUserName) {
self.changText = tempClose
}
點擊返回按鈕的回調方法我們要這樣寫
func pushClick() {
changText!(self.changName.text!)
self.navigationController?.popViewControllerAnimated(true)
}
在跳轉按鈕的方法里我們這樣寫(兩種方法,對不兩種不同屬性哦)
func ClickAction() {
let secondVC = SecondViewController()
// 防止循環引用
weak var WeakSelf = self
// 第一用方法
secondVC.changText = { (names) -> () in
print("------\(names)")
WeakSelf!.userNames!.text = names
}
// 第二用方法
// secondVC.setMyChangeName { (names) in
// print("------\(names)")
// WeakSelf!.userNames!.text = names
// }
self.navigationController?.pushViewController(secondVC, animated: true)
}