iOS很重要的 block回调
剛剛進(jìn)入ios開(kāi)發(fā)行業(yè),發(fā)現(xiàn)開(kāi)發(fā)中要用到大量的block回調(diào),由此可見(jiàn)它的重要性。學(xué)習(xí)它之前我也是網(wǎng)上找的資料,推薦這篇文章http://blog.csdn.net/mobanchengshuang/article/details/11751671,我也是從這里得到一點(diǎn)啟示。假設(shè)對(duì)block的使用還不熟悉建議先看我的block那篇文章。以下我用自己的project來(lái)解釋一下block回調(diào)函數(shù)。
一、先創(chuàng)建一個(gè)簡(jiǎn)單的xcodeproject
ViewController.h文件
//
//? ViewController.h
//? block回調(diào)
//
//? 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) {
?? ? ? ? /*函數(shù)回調(diào) 當(dāng)block運(yùn)行時(shí)就會(huì)回到這里*/
? ? ? ? _btnOutlet.backgroundColor = colorEnum;
? ? }];
}
@end
ShowBtnColor.h文件
//
//? ShowBtnColor.h
//? block回調(diào)
//
//? Created by pengxun523 on 14-4-22.
//? Copyright (c) 2014年 pengxun523. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef void (^Changcolor)(UIColor *colorEnum); //定義一個(gè)block返回值void參數(shù)為顏色值
@interface ShowBtnColor : NSObject
//回調(diào)函數(shù)改變btn的顏色值
+ (void)ChangeRootViewBtnRect:(CGRect)rect blockcompletion:(Changcolor)Changcolorblock;
@end
//
//? ShowBtnColor.m
//? block回調(diào)
//
//? 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); //運(yùn)行block?
}
@end
執(zhí)行結(jié)果?當(dāng)點(diǎn)擊button時(shí)?
轉(zhuǎn)載于:https://www.cnblogs.com/zfyouxi/p/4275152.html
總結(jié)
以上是生活随笔為你收集整理的iOS很重要的 block回调的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 吃菌子中毒吃什么好?
- 下一篇: 海花岛到三沙市多少公里?