//
// AppDelegate.h
// UI13_Block
//
// Created by l on 15/9/17.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
// UI13_Block
//
// Created by l on 15/9/17.
// Copyright (c) 2015年 . All rights reserved.
//
#import "AppDelegate.h"
#import "FirstViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc{
[_window release];
[super dealloc];
}
//定义一个函数
int sum(int x, int y){
return x + y;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//函数
//什么是函数? 就是有特定功能的代码段,函数也是一种数据类型,函数的类型有参数和返回值决定
//比如:int sum(int x, int y);
NSLog(@"%d", sum(2, 3));
//函数指针
//指针 :是一种类型,存放的是内存的地址
//指针变量: 指向自身存放地址的内存空间
//函数也是一种类型,指向函数这种类型的指针,被称为函数指针
//函数指针 通常指向一类函数,具有相同的参数和返回值的函数
//比如:int sum(int x, int y);
//int sub(int x, int y);
//int mul(int x, int y);
//定义了一个函数指针变量
int (*method)(int x, int y) = sum;
//调用
NSLog(@"%d", method(3, 5));
// 二. block
//block 为匿名函数,存储的是函数体,没有函数名
//block也是一种类型,为block类型,存储的是函数体
//block的标志是上划线(托字符) ^
//1.定义一个block类型的变量
//int (int x, int y)
int (^block1)(int x, int y);
//2.给block赋值
block1 = ^(int a, int b){
return a + b;
};
block1 = ^(int a, int b){
return a * b;
};
//3.调用
NSLog(@"%d", block1(10, 10));
//要求 定义一个block类型变量,block类型为一个NSString参数,返回值为NSInteger类型的block
//该block具有,将string转化为integer类型
NSInteger (^block2)(NSString *str);
block2 = ^(NSString *str){
return [str integerValue];
};
NSLog(@"%ld", block2(@"123"));
//block高级使用
//block主要用来当做参数进行函数回调以及界面间传值
//1.block作为方法的参数,函数回调
//2.实现界面间传值
//firstVC
FirstViewController *firstVC = [[FirstViewController alloc] init];
firstVC.title = @"第一个页面";
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:firstVC];
self.window.rootViewController = navC;
[navC release];
[firstVC release];
return YES;
}
@end
///
//
// FirstViewController.h
// UI13_Block
//
// Created by l on 15/9/17.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@end
//
// FirstViewController.m
// UI13_Block
//
// Created by l on 15/9/17.
// Copyright (c) 2015年 . All rights reserved.
//
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@property (nonatomic, retain) UITextField *textField;
@end
@implementation FirstViewController
- (void)dealloc{
[_textField release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
//textfield
_textField = [[UITextField alloc] initWithFrame:(CGRectMake(50, 100, 200, 50))];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor grayColor];
[self.view addSubview:_textField];
//nextBaarButton 下一个页面按钮
UIBarButtonItem *nextBarButton = [[UIBarButtonItem alloc] initWithTitle:@"next" style:(UIBarButtonItemStylePlain) target:self action:@selector(nextAction:)];
self.navigationItem.rightBarButtonItem = nextBarButton;
[nextBarButton release];
// Do any additional setup after loading the view.
}
//nextAction方法
- (void)nextAction:(UIBarButtonItem *)barButton{
//1.创建
SecondViewController *secondVC = [[SecondViewController alloc] init];
//2.设置属性 给第二个页面的block赋值,指定要实现的逻辑
//block赋值的时候,要注意 重复引用的问题
//我们不会在block直接使用_属性名,调用,而是通过一个弱引用的xxxself区调用自身的属性
//为了解决block的强引用造成的无法释放问题
//MRC
__block typeof (self) blockSelf = self;//typeof(self)作用是,返回一个弱引用的self,不会造成引用计数增加问题,因此,不会重复引用.
//ARC
__weak typeof (self) weakSelf = self;
secondVC.secondBlock = ^(NSString *text){
self.textField.text = text;
};
//3.push
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
- (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
//
// SecondViewController.h
// UI13_Block
//
// Created by l on 15/9/17.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
//起别名,MyBlock 指代void(^MyBlock) (NSString *string)类型
typedef void(^MyBlock) (NSString *string);
@interface SecondViewController : UIViewController
@property (nonatomic, retain) UITextField *textField;
//给第二个页面设置一个myblock类型的实例变量
@property (nonatomic, copy) MyBlock secondBlock;
//*** block类型为实例变量,必须用copy修饰
//把block变量,从栈区拷贝到堆区.
@end
//
// SecondViewController.m
// UI13_Block
//
// Created by l on 15/9/17.
// Copyright (c) 2015年 . All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)dealloc{
[_textField release];
//block release
Block_release(_secondBlock);
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
//textfield
_textField = [[UITextField alloc] initWithFrame:(CGRectMake(50, 100, 200, 50))];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor grayColor];
[self.view addSubview:_textField];
//backBarButton 返回上一级页面按钮
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction:)];
self.navigationItem.rightBarButtonItem = backBarButton;
[backBarButton release];
// Do any additional setup after loading the view.
}
//backAction方法
- (void)backAction:(UIBarButtonItem *)barButtonItem{
_secondBlock(_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
iOS编程------- Block高级------>Block传值
最新推荐文章于 2022-04-05 17:20:21 发布