美文网首页
代码块传值

代码块传值

作者: lichengjin | 来源:发表于2016-03-18 00:25 被阅读48次

#import<UIKit/UIKit.h>

#import "SecondViewController.h"

//遵循协议

@interface ViewController : UIViewController

@property(strong,nonatomic) UITextField *textName;

@property(strong,nonatomic) UIButton *btn;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.textName = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];

self.textName.borderStyle = 1;

[self.view addSubview:self.textName];

self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

self.btn.frame = CGRectMake(200, 200, 50, 50);

[self.btn setTitle:@"下一页" forState:UIControlStateNormal];

[self.btn addTarget:self action:@selector(NextPage) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:self.btn];

}

////实现协议方法

//-(void)postValue:(NSString *)info

//{

//    self.textName.text = info;

//}

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

if ([textField isFirstResponder]) {

[textField resignFirstResponder];

}

return YES;

}

-(void)NextPage

{

SecondViewController *second = [[SecondViewController alloc] init];

second.str = self.textName.text;

//实现代码块

second.myblock = ^(NSString *info){

self.textName.text = info;

};

[self presentViewController:second animated:YES completion:^{

NSLog(@"Next");

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

/* 代码块传值 从后往前传

1.声明代码块 (secondXXX.h)

2.声明一个代码块类型的属性 (SecondXXX.h)

3.调用代码块 (secondXXX.m)

4.实现代码块 (firstXXX.m)

*/

#import<UIKit/UIKit.h>

typedef void(^postValueBlock)(NSString *info);

@interface SecondViewController : UIViewController

@property(strong,nonatomic) NSString *str;

@property(strong,nonatomic) UITextField *textInfo;

@property(strong,nonatomic) postValueBlock myblock;

@end

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor grayColor];

NSLog(@"%@",self.str);

self.textInfo = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 100, 50)];

self.textInfo.borderStyle = 1;

self.textInfo.delegate = self;

self.textInfo.text = self.str;

[self.view addSubview:self.textInfo];

}

//3.调用代理方法

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{

//1.调用block

if (self.myblock) {

self.myblock(textField.text);

}

//2.键盘隐藏

if ([textField isFirstResponder]) {

[textField resignFirstResponder];

}

//3.隐藏页面

[self dismissViewControllerAnimated:YES completion:^{

NSLog(@"dismiss");

}];

return 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

相关文章

  • 代码块传值

    #import #import "SecondViewController.h" //遵循协议 @interfac...

  • Flutter_嵌套APP

    flutter 代码块 swift 传值 原生 to flutter 传值 flutte to 原生 写在后面...

  • OC与swift的数据传输

    简介 该项目主要介绍了oc与swift之间、swift内部几种常见的传值方式(属性传值、代码块传值、代理传值、通知...

  • iOS传值-反向传值-使用block(代码块)

    代码块:是一种OC的一种数据类型,在iOS4时被引入,具有匿名函数的特性。它可以保存一段代码,并在合适的时候取出来...

  • React refs和onRefs的使用

    父组件代码块 子组件代码块 父组件向子组件传值主要依靠父组件中parentMethods将方法和变量传递到子组件中...

  • iOS--《传值方法》之代码块Block传值

    第1步:在发送者中定义代码块属性: 第2步:在发送者中进行代码块的调用 第3步:在接收者中进行代码块的具体操作,既...

  • iOS页面传值

    常用 + 代码 属性传值 方法传值 Block Delegate NSNotificationCenter传参 N...

  • iOS 常用传值方式

    总结 iOS 日常开发中的几种常用传值方式:正向传值代理传值block传值通知传值单例 文章代码:https://...

  • 无标题文章

    block的反向传值 1. 调用方:准备块代码 跟“代理”来对比 - 类似于协议方法的实现 不同点:块代码都在一起...

  • 多线程2

    Block基本演练 block反向传值 主界面 准备等待执行的代码块 如果需要接收外界传入的值,需要定义参数 向目...

网友评论

      本文标题:代码块传值

      本文链接:https://www.haomeiwen.com/subject/qticlttx.html