iOS 传值

首页页面
首页
下一页页面
下一页
传值后首页页面
传值后

通知传值

- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册监听者
    // name 的值要和发送通知页面的name值相同,用来区分哪一个通知
    // object 的值为nil时,代表监听任何对象发送的通知,下文中用的是object:obj1
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValue:) name:@"changeValue" object:nil];
}

- (void)changeValue:(NSNotification *)notifi{
    // 可以传多值
    if ([notifi.object isEqualToString:@"obj1"]) {
        NSString *str = [NSString stringWithFormat:@"%@--%@",[[notifi userInfo] objectForKey:@"value1"],[[notifi userInfo] objectForKey:@"value2"]];
        _lbValue.text = str;
    }else {
        _lbValue.text = @"aaaaaa";
    }
}
// 按钮点击事件
- (IBAction)nextAction:(UIButton *)sender{
    NextViewController *nextController = [[NextViewController alloc] init];
    [self presentViewController:nextController animated:YES completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
}
// 按钮点击事件
- (IBAction)finishAction:(UIButton *)sender{
    // 发送通知
    // userInfo 为附加属性,为字典类型
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeValue" object:@"obj1" userInfo:@{@"value1":_tfText.text,@"value2":@"111111"}];
    [self dismissViewControllerAnimated:YES completion:nil];
}

代理传值

@interface ViewController () <ChangeValueDelegte>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];    
}
- (IBAction)nextAction:(id)sender{
    NextViewController *nextController = [[NextViewController alloc] init];
    // 遵守协议
    nextController.delegate = self;
    [self presentViewController:nextController animated:YES completion:nil];
}
// 实现协议中的方法
- (void)shoudChangeValue:(NSString *)paraStr{
    _lbValue.text = paraStr;
}
#import <UIKit/UIKit.h>
// 声明一个协议
@protocol ChangeValueDelegte <NSObject>
- (void)shoudChangeValue:(NSString *)paraStr;
@end


@interface NextViewController : UIViewController
@property (nonatomic,weak) IBOutlet UITextField *tfValue;
// 注意delegate用weak修饰,为防止循环引用
@property (nonatomic,weak) id<ChangeValueDelegte> delegate;
@end



- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)finishAction:(id)sender{
    // 让代理传值
    [_delegate shoudChangeValue:_tfValue.text];
    [self dismissViewControllerAnimated:YES completion:nil];
}

block传值

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (IBAction)nextAction:(id)sender{
    NextViewController *nextController = [[NextViewController alloc] init];
    // 实现block
    [nextController setChangeValue:^(NSString *param){
        _lbValue.text = param;
    }];
    [self presentViewController:nextController animated:YES completion:nil];
}
#import <UIKit/UIKit.h>
// 声明一个block
typedef  void(^ShoudChangeValue)(NSString *paraStr);

@interface NextViewController : UIViewController
@property (nonatomic,weak) IBOutlet UITextField *tfValue;
// block 用copy修饰
@property (nonatomic,copy) ShoudChangeValue changeValue;

@end


- (void)viewDidLoad {
    [super viewDidLoad];
}
- (IBAction)finishAction:(id)sender{
    // 调用block传值
    _changeValue(_tfValue.text);
    [self dismissViewControllerAnimated:YES completion:nil];
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值