YIem`s Blog -心比天高命比纸薄-链接找不到的请在站内搜索内容!

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

@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

}

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;

}

@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>

@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

warning 4.在触发方法时 调用block属性

self.textBlock(_textField.text);
[self.navigationController popViewControllerAnimated:YES];

}

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

*/

@end

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »