IOS中Notificaiton通知的使用方法<代码演示>

本文介绍如何在iOS应用中利用通知(NSNotification)进行消息传递,并通过ModalViewController展示通知的效果。通过实例展示了通知在不同视图间传递参数的方法,以及如何在ModalViewController中响应并使用接收到的通知来改变标签文本。

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

    //使用通知进行消息传递(通知NSNotificationCenter不可都用)
    
//NSNotificationCenter可以在毫无关系的视图中进行参数传值
    
//NSNotificationCenter是一个one-to-one &one-to-many &one-to- none的关系
    
//过多使用会降低代码的可读性,增加程序的耦合度


- (
void)viewDidLoad
{
    [
super viewDidLoad];
// Do any additional setup after loading the view.
    
self.view.backgroundColor = [UIColor purpleColor];
    
    
//实例化一个用于显示的Label
    
UILabel *label = [[UILabel allocinitWithFrame:CGRectMake(506410060)];
    label.
text = @"没变化";
    label.
backgroundColor = [UIColor whiteColor];
    label.
tag = 100;
    [
self.view addSubview:label];
    
RELEASE_SAFETY(label);
    
    
//实例化一个触发事件按钮
    
UIButton *presentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect ];
    presentButton.
backgroundColor = [UIColor whiteColor];
    presentButton.
frame = CGRectMake(5020020060);
    [presentButton 
setTitle:@"ModalViewController" forState:UIControlStateNormal];
    [presentButton 
addTarget:self 
                      action:@selector(presetAction) 
            forControlEvents:UIControlEventTouchUpInside];
    [
self.view addSubview:presentButton];
    
//使用通知进行消息传递(通知NSNotificationCenter不可都用)
    
//NSNotificationCenter可以在毫无关系的视图中进行参数传值
    
//NSNotificationCenter是一个one-to-one &one-to-many &one-to- none的关系
    
//过多使用会降低代码的可读性,增加程序的耦合度
    
//#define kChangeLabelValueByNotification @"kChangeLabelValueByNotification"宏定义
    [[
NSNotificationCenter defaultCenteraddObserver:self 
                                             selector:@selector(changeLabelText:)
                                                 name:kChangeLabelValueByNotification 
                                               object:nil];
    
    
}
#pragma mark==实现通知方法=
-(void)changeLabelText:(NSNotification *)notification{

    
//获取通知传递过来的对象
    
id label = notification.object;
    
//修改原有Label
    ((
UILabel *)[self.view viewWithTag:100]).text = label;



}
//实现模态方法
-(
void)presetAction{

    
ModalViewController *modal = [[ModalViewController allocinit];
    [
self presentViewController:modal animated:YES completion:nil];
    
RELEASE_SAFETY(modal);

}
//重写析构函数
-(
void)dealloc{

    [[
NSNotificationCenter defaultCenterremoveObserver:self 
                                                    name:kChangeLabelValueByNotification 
                                                  object:nil];
    [
super dealloc];
}


//ModalViewController.
- (
void)viewDidLoad
{
    [
super viewDidLoad];
// Do any additional setup after loading the view.
    
self.view.backgroundColor = [UIColor purpleColor];
    
//实例化一个用于显示的Label
    
self.textField = [[[UITextField allocinitWithFrame:CGRectMake(506410060)] autorelease];
    
self.textField.backgroundColor = [UIColor whiteColor];
    
self.textField.borderStyle = UITextBorderStyleRoundedRect;
    [
self.view addSubview:self.textField];
    
    
//实例化一个触发事件按钮
    
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect ];
    dismissButton.
backgroundColor = [UIColor whiteColor];
    dismissButton.
frame = CGRectMake(5020020060);
    [dismissButton 
setTitle:@"dismissViewController" forState:UIControlStateNormal];
    [dismissButton 
addTarget:self 
                      action:@selector(dismissAction) 
            forControlEvents:UIControlEventTouchUpInside];
    [
self.view addSubview:dismissButton];
    
}
-(
void)dismissAction{
    
    [[
NSNotificationCenter defaultCenterpostNotificationName:kChangeLabelValueByNotification 
                                                        object:self.textField.text];

    [
self dismissViewControllerAnimated:YES completion:nil];

}

-(
void)dealloc{
    
RELEASE_SAFETY(_textField);
    [
super dealloc];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GFanStudio-LeeSir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值