1.block 傳值
#import "ViewController.h"
#import "SendViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {? ? [super viewDidLoad];? ? ?
?? self.view.backgroundColor = [UIColor whiteColor];? ?
?UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];? ? temporaryBarButtonItem.title =@"返回";? ??
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
SendViewController*send = [[SendViewController alloc]init];
send.myblock = ^(NSString*str){
self.navigationItem.title = str;
};
[self.navigationController pushViewController:send animated:YES];
}
typedef void(^Myblock) (NSString*);
@interface SendViewController : UIViewController
@property(nonatomic,copy)Myblock myblock;
@interface SendViewController : UIViewController
@property(nonatomic,copy)Myblock my block;
2.block 對象方法傳值
@interface ViewController ()
@property(nonatomic,copy)UILabel*lable;
@end@implementation ViewController
- (void)viewDidLoad {? ? [super viewDidLoad];? ??
? ? [self.view addSubview:({? ? ? ? ? ? _lable = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-100, self.view.frame.size.height/2-50, 200, 100)];? ? ? ? ? ? ? ? _lable.backgroundColor = [UIColor lightGrayColor];? ? ? ? ? ? ? ? _lable.font = [UIFont systemFontOfSize:13.0];? ? ? ? ? ? ? ? _lable.text = @"你好";? ? ? ? ? ? ? ? _lable;? ? ? ? })];? ? ? ? }-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
SendViewController*sendVc = [[SendViewController alloc]init];
//^(NSString *str) {_lable.text = str;}把此處代碼放到SendViewController中保存
[sendVc SendViewContollerBlock:^(NSString *str) {
_lable.text = str;
}];
//? ? sendVc.myblockColor = ^(NSString*str){
//
//? ? ? ? _lable.text = str;
//? ? };
[self presentViewController:sendVc animated:YES completion:^{
sendVc.textField.text = _lable.text;
}];
}
#import typedef void(^MyblockColor)(NSString*);
@interface SendViewController : UIViewController
@property(nonatomic,copy)MyblockColor myblockColor;
@property(nonatomic,strong)UITextField*textField;
-(void)SendViewContollerBlock:(MyblockColor)myblockColor;
@interface SendViewController ()
@end
@implementation SendViewController
- (void)viewDidLoad {? ? ? ? [super viewDidLoad];? ? ? ?
?self.view.backgroundColor = [UIColor whiteColor];? ? ??
? [self.view addSubview:({? ? ? ? ? ? _textField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-100, self.view.frame.size.height/2-50, 200, 100)];? ? ? ?
?_textField.delegate = self;? ? ? ? ? ? ?
?? _textField.backgroundColor = [UIColor lightGrayColor];? ? ? ? ? ? ? ?
?_textField.placeholder = @"請輸入";? ? ? ? ? ? ? ? _textField;? ? ? ? ? ? ? ? })];? ? }
-(void)SendViewContollerBlock:(MyblockColor)myblockColor{? ? self.myblockColor = myblockColor;? ? }
- (void)viewWillDisappear:(BOOL)animated{? ? ? ? [super viewWillDisappear:animated];? ? }- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
//模泰推出
//alloc? init? loadview viewDidload viewwillApp viewdidapp viewwiilldisapp
[self dismissViewControllerAnimated:YES completion:^{
if (_myblockColor) {
_myblockColor(_textField.text);
}
}];
#import "ViewController.h"
#import "SendViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)blockButton:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
if (_button.tag != 101) {
_button.backgroundColor = [UIColor purpleColor];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)blockButton:(id)sender {
CGRect temp = CGRectMake(_button.frame.origin.x, _button.frame.origin.y, _button.frame.size.width+50,_button.frame.size.height+20);
[SendViewController ChangeRootViewBtnRect:temp blockcompletion:^(UIColor *ClolorEnume) {
_button.tag = 101;
_button.backgroundColor = ClolorEnume;
}];
}
#importtypedef void(^ChangeColor)(UIColor*ClolorEnume);
@interface SendViewController : NSObject
@property(nonatomic,copy)ChangeColor changeColor;
+(void)ChangeRootViewBtnRect:(CGRect)rect blockcompletion:(ChangeColor)changeblockColor;
@end
#import "SendViewController.h"
@interface SendViewController ()
@end
@implementation SendViewController
+ (void)ChangeRootViewBtnRect:(CGRect)rect blockcompletion:(ChangeColor)changeblockColor{
UIColor*temp = [UIColor redColor];
changeblockColor(temp);
}