美文网首页
UI总结-界面传值

UI总结-界面传值

作者: Dear丶Musk | 来源:发表于2016-05-19 19:26 被阅读112次

UI总结-界面传值(属性传值,协议传值,block传值,通知中指传值)

在编程过程中,界面传值是很重要的一部分,常用的传值方式就有四种:属性传值,协议传值,block传值,通知中指传值,下面通过简单的代码来实现这四种传值方式:

前一个界面ViewController.m文件:

#import "ViewController.h"

#import "SecondViewController.h"

//4.签订协议

@interface ViewController ()

@property(nonatomic, retain)UITextField *text;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

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

self.view.backgroundColor = [UIColor whiteColor];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

btn.backgroundColor = [UIColor redColor];

[self.view addSubview:btn];

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

btn.layer.borderWidth = 1;

btn.layer.cornerRadius = 5;

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

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

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

self.text.backgroundColor = [UIColor blueColor];

[self.view addSubview:self.text];

[_text release];

self.text.layer.borderWidth = 1;

self.text.layer.cornerRadius = 5;

//第四种传值方式:通知中心

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(jieshou:) name:@"text" object:nil];

}

-(void)click:(UIButton *)button{

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

[self.navigationController pushViewController:vc animated:YES];

//属性传值

//在要传值的后一个页面定义属性,在对应的视图控制器调用属性进行赋值,就可以将值传到后一个页面

vc.str = self.text.text;

vc.arr = @[@"james", @"alice", @"tom"];

//5.设置代理人

vc.delegate = self;

//先写一个block

void(^block)() = ^(){

self.view.backgroundColor = [UIColor blueColor];

};

//传值

vc.block = block;

[vc release];

}

//6.实现代理方法

-(void)sendValue:(NSString *)str{

self.title = str;

}

-(void)jieshou:(NSNotification *)notification{

self.text.text = [notification.userInfo valueForKey:@"name"];

}

后一个界面SecondViewController.h文件:

#import

//1.声明协议

@protocol SecondViewControllerDelegate

-(void)sendValue:(NSString *)str;

@end

@interface SecondViewController : UIViewController

@property(nonatomic, retain)NSString *str;

@property(nonatomic, retain)NSArray *arr;

//2.设置代理人属性@property(nonatomic, assign)iddelegate;

//block传值

@property(nonatomic, copy)void(^block)(void);

@end

后一个界面SecondViewController.m文件:

#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic, retain)UITextField *text;

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor whiteColor];

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

self.title = self.str;

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

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

btn.backgroundColor = [UIColor redColor];

[self.view addSubview:btn];

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

btn.layer.borderWidth = 1;

btn.layer.cornerRadius = 5;

[btn setTitle:@"返回" forState:UIControlStateNormal];

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

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

self.text.backgroundColor = [UIColor blueColor];

[self.view addSubview:self.text];

[_text release];

self.text.layer.borderWidth = 1;

self.text.layer.cornerRadius = 5;

}

-(void)click:(UIButton *)button{

[self.navigationController popViewControllerAnimated:YES];

//3.触发协议生效

[self.delegate sendValue:self.text.text];

//调用block

self.block();

[[NSNotificationCenter defaultCenter]postNotificationName:@"text" object:nil userInfo:@{@"name":@"james"}];

}

运行结果如下:

相关文章

  • UI总结-界面传值

    UI总结-界面传值(属性传值,协议传值,block传值,通知中指传值) 在编程过程中,界面传值是很重要的一部分,常...

  • UI总结-tableView的界面传值

    UI总结-tableView的界面传值 因为tableView在以后的开发占了很重要的地位,所以把table...

  • UI界面传值

    传值需求 将用户信息 userInfo 作为传值对象进行传递。 场景一 主页传值到详情页 现在模拟传递用户名:us...

  • iOS中界面传值的几种方式

    1.属性传值 属性传值适用于顺序传值,从前面的界面传值给后面的界面。 2.代理传值 用代理的方式实现界面间传值稍微...

  • IOS开发 多界面传值

    本节学习内容: 1.多界面传值的基本概念 2.多界面传值的方法 3.多界面传值的应用 【多界面传值 属性】 cha...

  • ios界面传值2016.5

    五种方法 1.属性传值,适合界面A到界面B的传值2.单例, 多个界面传值3.通知 , 界面A跳...

  • iOS开发-属性、block、代理、通知传值

    传值在开发中我们会经常用到,传值又分为正向传值和反向传值。从界面一跳转到界面二且将值从界面一传递给界面二使用,称之...

  • iOS 属性、代理、通知、Block传值

    实际开发中,几乎到处都会有用到传值,而传值分为正向传值以及逆(反)向传值,比如从界面一调到界面二,并将值从界面一传...

  • swift中的协议代理传值

    协议代理传值一般使用在下级界面往上级界面传值的情况,这里将上级界面设定为A界面,下级界面设定为B界面。传值的具体操...

  • iOS的5种传值

    (-)属性传值 属性传值(场景)一般用于正向传值,即第一个界面传值给第二个界面 属性传值是这几大传值中最简单的传值...

网友评论

      本文标题:UI总结-界面传值

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