iOS很重要的 block回调
剛剛進入ios開發行業,發現開發中要用到大量的block回調,由此可見它的重要性。學習它之前我也是網上找的資料,推薦這篇文章http://blog.csdn.net/mobanchengshuang/article/details/11751671,我也是從這里得到一點啟示。假設對block的使用還不熟悉建議先看我的block那篇文章。以下我用自己的project來解釋一下block回調函數。
一、先創建一個簡單的xcodeproject
ViewController.h文件
//
//? ViewController.h
//? block回調
//
//? Created by pengxun523 on 14-4-16.
//? Copyright (c) 2014年 pengxun523. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *btnOutlet;
- (IBAction)btnClick:(UIButton *)sender;
@end
#import "ViewController.h"
#import "ShowBtnColor.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
? ? [super viewDidLoad];
}
-(void)chargeMyIphone:(void(^)(void))finishBlock
{
}
- (void)didReceiveMemoryWarning
{
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
- (IBAction)btnClick:(UIButton *)sender
{
? ? CGRect temp = CGRectMake(_btnOutlet.frame.origin.x, _btnOutlet.frame.origin.y, _btnOutlet.frame.size.width+50, _btnOutlet.frame.size.height+20);
?? ?
? ? [ShowBtnColor ChangeRootViewBtnRect:temp blockcompletion:^(UIColor *colorEnum) {
?? ? ? ? /*函數回調 當block運行時就會回到這里*/
? ? ? ? _btnOutlet.backgroundColor = colorEnum;
? ? }];
}
@end
ShowBtnColor.h文件
//
//? ShowBtnColor.h
//? block回調
//
//? Created by pengxun523 on 14-4-22.
//? Copyright (c) 2014年 pengxun523. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef void (^Changcolor)(UIColor *colorEnum); //定義一個block返回值void參數為顏色值
@interface ShowBtnColor : NSObject
//回調函數改變btn的顏色值
+ (void)ChangeRootViewBtnRect:(CGRect)rect blockcompletion:(Changcolor)Changcolorblock;
@end
//
//? ShowBtnColor.m
//? block回調
//
//? Created by pengxun523 on 14-4-22.
//? Copyright (c) 2014年 pengxun523. All rights reserved.
//
#import "ShowBtnColor.h"
@implementation ShowBtnColor
+ (void)ChangeRootViewBtnRect:(CGRect)rect blockcompletion:(Changcolor)Changcolorblock
{
? ? UIColor *temp = [UIColor greenColor];
? ? Changcolorblock(temp); //運行block?
}
@end
執行結果?當點擊button時?
轉載于:https://www.cnblogs.com/zfyouxi/p/4275152.html
總結
以上是生活随笔為你收集整理的iOS很重要的 block回调的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吃菌子中毒吃什么好?
- 下一篇: FPGA异步时钟设计中的同步策略