
正文
页面传值总结Block
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
// AppDelegate.m
// 页面传值总结
//
// Created by qianfeng on 15/6/13.
// Copyright (c) 2015年 qianfeng. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ViewController *ctr = [[ViewController alloc]init];
UINavigationController*nav = [[UINavigationController alloc]initWithRootViewController:ctr];
self.window.rootViewController = nav; return YES;
}
//
// ViewController.m
// 页面传值总结
//
// Created by qianfeng on 15/6/13.
// Copyright (c) 2015年 qianfeng. All rights reserved.
// #import "ViewController.h"
#import "DetailVIewController.h" @interface ViewController ()
{
UILabel*_label; }
@property(nonatomic,strong)UILabel*label; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; //用于显示第二个界面传过来的数据。
_label = [[UILabel alloc]initWithFrame:CGRectMake(120, 220, 100, 40)];
_label.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_label]; //创建用于跳转到第二个界面的按钮;
UIButton*btn = [self createBtnFrame:CGRectMake(120, 300, 100, 40) title:@"跳转" target:self action:@selector(clickBtn:)];
btn.backgroundColor = [UIColor grayColor];
[self.view addSubview:btn]; } -(void)clickBtn:(id)sender
{
DetailVIewController*ctr = [[DetailVIewController alloc]init];
__weak ViewController*weakself =self;
ctr.clockBlock = ^(NSString*title){
weakself.label.text = title; };
[self.navigationController pushViewController:ctr animated:YES]; } -(UIButton*)createBtnFrame:(CGRect)frame title:(NSString*)title target:(id)target action:(SEL)action
{
UIButton*btn= [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = frame;
[btn setTitle:title forState:UIControlStateNormal];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; return btn; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
//
// DetailVIewController.h
// 页面传值总结
//
// Created by qianfeng on 15/6/13.
// Copyright (c) 2015年 qianfeng. All rights reserved.
// #import <UIKit/UIKit.h> @interface DetailVIewController : UIViewController //声明block
@property(nonatomic,copy)void(^clockBlock)(NSString*titler); @end //
// DetailVIewController.m
// 页面传值总结
//
// Created by qianfeng on 15/6/13.
// Copyright (c) 2015年 qianfeng. All rights reserved.
// #import "DetailVIewController.h" @implementation DetailVIewController -(void)viewDidLoad
{
[super viewDidLoad] ;
self.view.backgroundColor = [UIColor whiteColor]; UIButton*bt1 = [self createBtnFrame:CGRectMake(200, 140, 100, 40) title:@"block传值" target:self action:@selector(blockClick:)];
[self.view addSubview:bt1]; }
-(void)blockClick:(UIButton*)sender
{
NSString*title = [sender currentTitle]; if (self.clockBlock) {
self.clockBlock(title);
}
}
-(UIButton*)createBtnFrame:(CGRect)frame title:(NSString*)title target:(id)target action:(SEL)action
{
UIButton*btn= [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = frame;
[btn setTitle:title forState:UIControlStateNormal];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; return btn; } @end








