呜呜呜。。。刚才出去看看隔壁公司的前台,写完了吧
重点在 (3) 这里 block 的变量的使用要主要啊,要不然引起问题可是很不好解决的
(1)
//
// ViewController.h
// xunHuan
//
// Created by jie on 15/3/26.
// Copyright (c) 2015年 jie. All rights reserved.
//
import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// xunHuan
//
// Created by jie on 15/3/26.
// Copyright (c) 2015年 jie. All rights reserved.
//
import "ViewController.h"
import "OneViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];self.view.backgroundColor = [UIColor orangeColor];
self.title = @"View";
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 120, 40);
[btn setTitle:@"Press Action" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(pressAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[OneViewController transferClassMethod:^(int a, NSString *s) {
NSLog(@"ViewController = %d %@",a,s);
self.view.backgroundColor = [UIColor brownColor];
}];
}
-(void)pressAction
{
OneViewController *one = [[OneViewController alloc] init];
[self.navigationController pushViewController:one animated:YES];
}
-(void)dealloc
{
NSLog(@"dealloc ViewController");
}
@end
(2)
//
// OneViewController.h
// xunHuan
//
// Created by jie on 15/3/26.
// Copyright (c) 2015年 jie. All rights reserved.
//
import <UIKit/UIKit.h>
typedef void(^myBlock) (int a,NSString *s);
@interface OneViewController : UIViewController
+(void)transferClassMethod:(myBlock)block;
-(void)exampleClassMethod:(myBlock)block;
@end
//
// OneViewController.m
// xunHuan
//
// Created by jie on 15/3/26.
// Copyright (c) 2015年 jie. All rights reserved.
//
import "OneViewController.h"
import "TwoViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"One";
self.view.backgroundColor = [UIColor redColor];
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(100, 200, 120, 40);
[bt setTitle:@"Preess One" forState:UIControlStateNormal];
[bt addTarget:self action:@selector(ppppp) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt];
}
-(void)ppppp
{
TwoViewController *two = [[TwoViewController alloc] init];
[self.navigationController pushViewController:two animated:YES];
}
+(void)transferClassMethod:(myBlock)block
{
block(100,@"zhaojie");
}
-(void)dealloc
{
NSLog(@"dealloc OneViewController");
}
-(void)exampleClassMethod:(myBlock)block
{
block(99,@"exampleMethod");
}
@end
(3)
//
// TwoViewController.h
// xunHuan
//
// Created by jie on 15/3/26.
// Copyright (c) 2015年 jie. All rights reserved.
//
import <UIKit/UIKit.h>
@interface TwoViewController : UIViewController
@property(nonatomic,retain)NSString *propertyString;
@end
//
// TwoViewController.m
// xunHuan
//
// Created by jie on 15/3/26.
// Copyright (c) 2015年 jie. All rights reserved.
//
import "TwoViewController.h"
import "OneViewController.h"
import "BlockButton.h"
@interface TwoViewController ()
{
NSString *blockString;
}
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Two";
self.view.backgroundColor = [UIColor blueColor];
blockString = @"blcokString";
self.propertyString = @"proString";
NSLog(@"startValue %@ %@ ",self.propertyString,blockString);
pragma mark--------------------------
// NSString *partString = @"partString"; // 不可改变在函数内不会改变
// __block NSString *partString = @"partString"; // 可改变在函Blcok内,但是在内部改变的值之后不会改变变量在外边的值
//#pragma mark 1
// [OneViewController transferClassMethod:^(int a, NSString s) {
// blockString = @"changBlockString";
// self.propertyString = @"ChangeproString";
// NSLog(@"ChangValue %@ %@",self.propertyString,blockString);
// }];
// NSLog(@"startValue %@ %@ ",self.propertyString,blockString);
// /*
// startValue proString blcokString
// ChangValue ChangeproString changBlockString
// startValue ChangeproString changBlockString
// viewWillDisappear changBlockString ChangeproString
// */
//#pragma mark 2
// OneViewController one = [[OneViewController alloc] init];
// [one exampleClassMethod:^(int a, NSString s) {
//
// blockString = @"2changeBlockString";
// self.propertyString = @"2ChangeProString";
// NSLog(@"ChangValue %@ %@",self.propertyString,blockString);
// }];
// NSLog(@"startValue %@ %@ ",self.propertyString,blockString);
//
// /
// *
// startValue proString blcokString
// ChangValue 2ChangeProString 2changeBlockString
// startValue 2ChangeProString 2changeBlockString
// viewWillDisappear 2changeBlockString 2ChangeProString
// */
pragma mark ------- 3
__weak TwoViewController *controller = self;
BlockButton *btn = [BlockButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(100, 100, 100, 100);
btn.backgroundColor = [UIColor brownColor];
[btn setTitle:@"Press Me" forState:UIControlStateNormal];
btn.buttonBlock = ^(){
// blockString = @"changeBlockString"; // 引用的话会引起循环引用 (1)如果 ======== __block NSString *ppp = blockString;这样的话使用全局变量 不会引起循环引用, 但是block变量值得改变 不会引起 blockString 的改变 (2) 不要这样子使用 应当用属性的的方
controller.propertyString = @"proString";
};
[self.view addSubview:btn]; // 此时当前控制器持有 btn 如果出现btn持有当前控制器时。。。可能引起循环引用的情况
}
-(void)viewWillDisappear:(BOOL)animated
{
NSLog(@"viewWillDisappear %@ %@",blockString,self.propertyString);
}
-(void)dealloc
{
NSLog(@"dealloc TwoViewController");
}
@end
(4)
//
// BlockButton.h
// blockButton
//
// Created by jie on 15/3/26.
// Copyright (c) 2015年 jie. All rights reserved.
//
import <UIKit/UIKit.h>
@class BlockButton;
typedef void(^myButtonBlock) ();
@interface BlockButton : UIButton
@property(nonatomic,copy)myButtonBlock buttonBlock;
@end
//
// BlockButton.m
// blockButton
//
// Created by jie on 15/3/26.
// Copyright (c) 2015年 jie. All rights reserved.
//
import "BlockButton.h"
@implementation BlockButton
@synthesize buttonBlock;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void)press
{
self.buttonBlock();
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
// 补充一句 block 可以理解成一种类型 一种对象类型
// Block就是OC的对象
哎哎哎哎哎哎哎 不是写博客的料啊 可不容易想写一次。。。。。。。。。写这个逼样的
最后这里边留下了 一个 bug
NSString *pargma = @"pargma";
__block NSString *partString = @"partString";
__block NSString *replaceString = blockString;// 全局的 ?
多想想吧
提示:__block 重写该自动变量也不会改变原先的截取的自动变量 ===== 反之亦然
那天补上。。。。。。 给说明白了 __block
网友评论