Block传值Block传值Block传值Block传值
//
// AppDelegate.h
// UI13_Block传值
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//
import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
// UI13_Block传值
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//
import "AppDelegate.h"
import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewController *root = [[ViewController alloc]init];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:root];
UITabBarController *tab = [[UITabBarController alloc]init];
root.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1000];
tab.viewControllers = @[navi];
self.window.rootViewController = tab;return YES;
} - (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
} - (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
} - (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
} - (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
} - (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
//
// ViewController.h
// UI13_Block传值
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//
import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// UI13_Block传值
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//
import "ViewController.h"
import "SecondViewController.h"
@interface ViewController ()<PassDelegate>
@property (nonatomic, retain)UITextField *textField;
@end
NSInteger global = 100;// 全局变量
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; self.navigationController.title = @"首页"; // textfield self.textField = [[UITextField alloc]init]; _textField.backgroundColor = [UIColor redColor]; _textField.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:_textField]; //BUtton UIButton *button = [[UIButton alloc]init]; button.backgroundColor = [UIColor yellowColor]; [self.view addSubview:button]; button.frame = CGRectMake(200, 200, 100, 100); [button addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
}
(void)button
{
// block 快语法
// block1 block名字(类似函数指针名)
// NSString *(^)(NSInteger) block类型 (nsinteger 参数)
// ^NSString *(NSInteger num){} block初值(函数匿名体)
NSString (^block1)(NSInteger num) = ^NSString (NSInteger num){return [NSString stringWithFormat:@"%ld", num];
};// 有返回有参
// 调用(类似函数调用)
NSString *numStr = block1(100);
NSLog(@"%@", numStr);// 无返回无参
void(^block2)(void) = ^(void){NSLog(@"lalala");
};
block2();
// 无返回有参
void (^block3)(NSString ) = ^(NSString str){NSLog(@"%@", str);
};
block3(@"123");
// 有返回无参
NSString *(^block4)(void) = ^(){return @"abc";
};
NSLog(@"%@", block4());SecondViewController *secVC = [[SecondViewController alloc]init];
secVC.str = _textField.text;
secVC.delegate = self;
warning block中 不能修改局部变量 如果需要修改 需要__block修饰
__block NSInteger i = 0 ;
void (^testBlock)(NSInteger) = ^(NSInteger num){
num++;
i = 10;
global = 10;
};
// 解决block访问局部变量的问题
// arc-> __weak
// mrc-> __block
__weak ViewController *vc = self;
// 改变颜色 block
void (^changeColorBlock)(UIColor *) = ^(UIColor *color){
vc.view.backgroundColor = color;
};
// 把遥控器 传递给第二页
secVC.block = changeColorBlock;
warning 1. 定义block用于改变第一页的内容
void(^changeText)(NSString *) = ^(NSString *string){
vc.textField.text = string;
};
warning 3. 将定义好的block通过属性的方法 赋值给第二页对象
secVC.textBlock = changeText;// 赋值
[self.navigationController pushViewController:secVC animated:YES];
// block可以出现在: 全局,栈,堆
// 不使用外部变量时 在全局区
NSLog(@"%@", block1);
// 使用了外部变量时 在栈区(MRC下在栈区 ARC下在堆区)
NSLog(@"%@", testBlock);
// copy后的block在堆区
}
-(void)passVlue:(NSString *)string
{
_textField.text = string;
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.
}
@end
//
// SecondViewController.h
// UI13_Block传值
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//
import <UIKit/UIKit.h>
@protocol PassDelegate <NSObject>
- (void)passVlue:(NSString *)string;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, strong)NSString *str;
@property (nonatomic, assign)id<PassDelegate>delegate;
//遥控器属性(block属性) // 接收
@property (nonatomic, copy) void(^block)(UIColor *);
warning 2.在第二页定义block属性
@property (nonatomic, copy) void(^textBlock)(NSString *);
@end
//
// SecondViewController.m
// UI13_Block传值
//
// Created by YIem on 15/12/23.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//
import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic, retain)UITextField *textField;
@end
@implementation SecondViewController
(void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor greenColor]; self.navigationItem.title = @"第二页"; // textField self.textField = [[UITextField alloc]init]; _textField.text = self.str; _textField.backgroundColor = [UIColor blueColor]; _textField.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:_textField]; // UIButton UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(200, 200, 100, 100)]; button.backgroundColor = [UIColor yellowColor]; [self.view addSubview:button]; [button addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
}
(void)button
{[self.delegate passVlue:_textField.text]; // 调用遥控器属性 改变第一页背景颜色 self.block([UIColor blackColor]);
warning 4.在触发方法时 调用block属性
self.textBlock(_textField.text);
[self.navigationController popViewControllerAnimated:YES];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.
}
/*
pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.
}
*/
@end