Block传值
来源:互联网 发布:知轻重明得失的意思 编辑:程序博客网 时间:2024/04/30 10:51
// MyViewController.m
// Block
//
// Created by cfy on 14-11-16.
//
//
#import "MyViewController.h"
#import "SecondViewController.h"
@interface MyViewController ()
{
UILabel * _label;
}
@end
@implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[_label release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.title = @"第一页";
// self.view.userInteractionEnabled = YES;
UIButton * butn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
butn.frame = CGRectMake(50, 150, 100, 20);
butn.backgroundColor = [UIColor orangeColor];
[butn setTitle:@"跳转" forState:UIControlStateNormal];
[butn addTarget:self action:@selector(pushPage) forControlEvents:UIControlEventTouchUpInside];
[butn addTarget:self action:@selector(pushPage) forControlEvents:UIControlEventTouchUpInside];
butn.userInteractionEnabled = YES;
[self.view addSubview:butn];
_label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
_label.backgroundColor = [UIColor greenColor];
_label.text = @"fsadfadf";
[self.view addSubview:_label];
}
-(void)pushPage
{
SecondViewController * seVc = [[SecondViewController alloc]init];
seVc.blocks = ^(NSString * str){
_label.text = str;
};
[self.navigationController pushViewController:seVc animated:YES];
[seVc 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
// Block
//
// Created by cfy on 14-11-16.
//
//
#import <UIKit/UIKit.h>
typedef void(^block) (NSString * str);
@interface SecondViewController : UIViewController
@property(nonatomic,copy)block blocks;
@end
// SecondViewController.m
// Block
//
// Created by cfy on 14-11-16.
//
//
#import "SecondViewController.h"
#import "MyViewController.h"
@interface SecondViewController ()
{
UITextField * _textField;
}
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[_textField release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
self.navigationItem.title = @"第二页";
UIButton * butn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
butn.frame = CGRectMake(100, 120, 50, 50);
butn.backgroundColor = [UIColor redColor];
[butn setTitle:@"返回" forState:UIControlStateNormal];
[butn addTarget:self action:@selector(backPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:butn];
//创建一个输入框
_textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
}
-(void)backPage
{
_blocks(_textField.text);
[self.navigationController popToRootViewControllerAnimated: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
- Block传值
- block传值
- Block 传值
- Block传值
- block传值
- block传值
- block传值
- Block传值
- Block 传值
- Block传值
- block传值
- block传值
- Block传值
- Block 传值
- Block 传值
- Block传值
- Block传值
- Block 传值
- Android开发之MediaPlayer详解
- 软件工程阶段性总结(一)——概述
- 最短路径—Dijkstra算法和Floyd算法
- LigerUI
- 根据pox和pyretic的例子,写自己的firewall
- Block传值
- 数组循环移位
- 关于字符数组的初始化
- U3D第二天学习心得
- 第12周项目3-输出输入的数的二进制形式
- gradle纪录
- JNI编程参考备忘----成员变量和方法
- POJ 3267-The Cow Lexicon(DP)
- 动态规划经典五题