IOS学习 Notification 用通知传值

本文介绍了一个iOS应用中如何使用通知中心实现视图控制器间的数据传递案例。具体包括两个视图控制器之间的交互:从主页视图控制器通过按钮跳转到第二个视图控制器,在第二个视图控制器中用户可以输入文本,当点击返回按钮后,输入的文本会通过通知传递回主页视图控制器并更新界面上的标签内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

#define kCLTN @"ChangLabelTextNotification"   //通知名

#define VIEW_WIDTH self.view.bounds.size.width

#define VIEW_HEIGHT self.view.bounds.size.height


@interface HomeViewController : UIViewController

@end



@implementation HomeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(VIEW_WIDTH/2-50, 300, 100, 40)];

    [btn setTitle:@"present" forState:UIControlStateNormal];

    btn.tintColor = [UIColor whiteColor];

    btn.backgroundColor = [UIColor purpleColor];

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

    [self.view addSubview:btn];

    

    UILabel *Label = [[UILabel alloc]initWithFrame:CGRectMake(VIEW_WIDTH/2-100, 160, 200, 40)];

    Label.backgroundColor = [UIColor orangeColor];

    Label.text = @"yihong";

    Label.tag = 102;

    [self.view addSubview:Label];

    

    //向数据中心了注册了一个通知,ChangLabelTextNotification

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changLabelText:) name:kCLTN object:nil];

}

//传值设置

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

    //根据tag查找label

    UILabel *label = (UILabel *)[self.view viewWithTag:102];

    label.text = notification.object;

}


//页面跳转

- (void)click {

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

    [self presentViewController:secondVC animated:YES completion:^{NSLog(@"present");}];

}




#import "HomeViewController.h"


@interface SecondViewController : UIViewController

{

@private

    UITextField *_textField;

}

@end




@implementation SecondViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    _textField = [[UITextField alloc]initWithFrame:CGRectMake(VIEW_WIDTH/2-100, 160, 200, 40)];

    _textField.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_textField];

    

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(VIEW_WIDTH/2-50, 300, 100, 40)];

    [btn setTitle:@"dismiss" forState:UIControlStateNormal];

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

    btn.backgroundColor = [UIColor purpleColor];

    btn.layer.cornerRadius = 6;

    [self.view addSubview:btn];

}


- (void)dismiss{

    //监听homeVC上的通知

    [[NSNotificationCenter defaultCenter]postNotificationName:kCLTN object:_textField.text];

    //模态视图返回主页面

    [self dismissViewControllerAnimated:YES completion:^{

        NSLog(@"dismiss");}];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值