ios 界面传值

下面我们讲讲几种比较常见的界面传值:正向传值(属性传值),block传值,代理传值,单例传值,通知传值。

首先 创建一个ViewController ,在创建一个跳转的控制器 FirstViewController
一、正向传值

1、首先在 FirstViewController.h 中 声明一个属性 (可以是NSString,NSArray,NSDictionary)

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
@property (nonatomic, copy) NSString * stringData;
@property (nonatomic, strong) NSArray * array;
@property (nonatomic, strong) NSDictionary * dict;
@end

2、在 FirstViewController.m 打印传的值

NSLog(@"正向传值------%@",self.stringData);

3、在ViewController.m 中

UIButton * first = [UIButton buttonWithType:UIButtonTypeCustom];
first.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 100, 120, 200, 50);
[first setTitle:@"正向传值" forState:UIControlStateNormal];
[first addTarget:self action:@selector(toViewController:) forControlEvents:UIControlEventTouchUpInside];
first.titleLabel.font = [UIFont systemFontOfSize:15];
first.backgroundColor = [UIColor redColor];
[self.view addSubview:first];

- (void)toViewController:(UIButton *)sender {
    FirstViewController * first = [[FirstViewController alloc] init];
    first.stringData = @"正向传的值";
    [self.navigationController pushViewController:first animated:YES];
}

运行
在这里插入图片描述
二、block 传值

1、在FirstViewController.h 中

  @property (nonatomic, copy) void(^ myBlock) (NSString *);

2、在FirstViewController.m 中 在按钮的点击事件中,直接调用
self.myBlock(@“block传的值”);
3、在ViewController.m 中定义一个 UILable * changeLable;用来接受block传的值

    changeLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 380, [UIScreen mainScreen].bounds.size.width, 60)];
    changeLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:changeLable];

   FirstViewController * first = [[FirstViewController alloc] init];
    first.stringData = @"正向传的值";
    first.myBlock = ^(NSString * string) {
        changeLable.text = string;
    };
    [self.navigationController pushViewController:first animated:YES];

运行结果
在这里插入图片描述
三、代理

1、在FirstController.h 定义代理

@protocol PushToContentDelegate <NSObject>
- (void)pushToConttent:(id)content;
@end
@property (nonatomic, assign) id<PushToContentDelegate> delegate;

2、在FirstController.m中

// 通过代理传值首次判断代理是否存在,并在代理能够响应代理方法时才执行代理方法
if (self.delegate && [self.delegate respondsToSelector:@selector(pushToConttent:)]) {
   [self.delegate pushToConttent:@"代理传的值"];
}

3、在ViewController.m中 , 实现该代理,并实现该代理的方法

@interface ViewController () <PushToContentDelegate>
{
    UILabel * changeLable;
}
@end

- (void)toViewController:(UIButton *)sender {
    FirstViewController * first = [[FirstViewController alloc] init];
    first.stringData = @"正向传的值";
    first.delegate = self;
    [self.navigationController pushViewController:first animated:YES];
}

- (void)pushToConttent:(id)content {
    changeLable.text = [NSString stringWithFormat:@"%@",content];
}

运行效果
在这里插入图片描述
四、通知

1、在ViewController.m 中注册一个通知监听,并在页面消失时移除该通知

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

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)notificeM : (NSNotification *)nof {
    NSDictionary * dict = nof.userInfo;
    changeLable.text = [NSString stringWithFormat:@"%@",dict[@"content"]];
}

2、在FirstViewController.m 中 发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@“PUSHTOCONTENT” object:self userInfo:@{@“content”?“通知传值”}];

运行结果
在这里插入图片描述

总结

  • 属性传值就是在一个页面给另一个页面的属性赋值,通过赋值的这个属性来传递的信息;
  • block传值和属性传值差不多,block代码块也是另一个界面的属性,block将值保存在代码块中,通过页面的回调代码块,以获取代码块传递过来的值。
  • 代理方法是用于任意界面之间传值,只需要声明实现代理方法,就可以获取传递过来的值其实
  • 注册通知与移除通知需要一一对应,同时通知名称要相同,才能收到该通知发送的消息。

scdn demo下载

GitHub demo下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值