代码改变世界

iOS block在两个页面间的简单传值

2015-12-01 14:49  甘雨路  阅读(190)  评论(0编辑  收藏  举报
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    // 给根控制器添加导航栏
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
    self.window.rootViewController = navigationController;
    
    
    [self.window makeKeyAndVisible];
    return YES;
}

@end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
#import "RootViewController.h"
#import "BViewController.h"
@interface RootViewController ()
{
    BViewController *bVC;
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 初始化button
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 150, 150, 70);
    btn.backgroundColor = [UIColor greenColor];
    [btn setTitle: @"PUSH到下一个控制器" forState:0];
    [btn setTitleColor:[UIColor orangeColor] forState:0];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    //初始化label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 70, 150, 70)];
    label.backgroundColor = [UIColor redColor];
    label.textColor = [UIColor yellowColor];
    [self.view addSubview:label];
    //初始化BViewController
    bVC = [[BViewController alloc] init];
    
    // 声明block(注意:这里只是声明,并没有执行block的代码,只有当block被调用才执行下面的代码)
    bVC.titleString = ^(NSString *title){
        label.text = title;
    };
}

- (void)btnAction:(UIButton *)sender{
    // push到下一个控制器
    [self.navigationController pushViewController:bVC animated:YES];
}



@end
#import <UIKit/UIKit.h>

typedef void(^titleBlock)(NSString *showText);

@interface BViewController : UIViewController
// 注意:copy 把block从栈区推到堆区
@property (nonatomic, copy) titleBlock titleString;

@end
#import "BViewController.h"

@interface BViewController ()
{
    UITextField *tf;
}
@end

@implementation BViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 初始化tf
    tf = [[UITextField alloc] initWithFrame:CGRectMake(80, 100, 150, 30)];
    tf.backgroundColor = [UIColor lightGrayColor];
    tf.text = @"把值传到根控制器";
    tf.textColor = [UIColor yellowColor];
    [self.view addSubview:tf];
}
/**
 *  当页面将要消失时,传值到根控制器
 */
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (tf.text.length != 0) {
        //执行block的代码(当执行此语句,就会跳到RootViewController的block声明的地方,执行block的代码)
        self.titleString(tf.text);
    }
}


@end