温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS 开发 Notification传值

发布时间:2020-09-21 09:15:10 阅读:588 作者:xyjn332 栏目:移动开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

通知传值

第一个UIViewcontroller.h

#import <UIKit/UIKit.h>@interface UseNotifi_VCOne :UIViewController@end

UIViewcontroller.m

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.            //监听某个通知    //这里需要注意到 : 通知只能是 对象才可用,且该对象必须存在于内存中    /*     [NSNotificationCenter defaultCenter] 获取通知的管理     addObserver: 设置通知的监听者      */    //NOTIFICATION_CHANHECONTENT 在 QFUseNotifi_VCTwo中    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(contentChange:) name:NOTIFICATION_CHANGECONTENT object:nil];    }-(void)dealloc{    //       通知    //         |   //         \ /  //    对象-->监听<--通知 //  将当前对象监听的所有通知移除    [[NSNotificationCenter defaultCenter]  removeObserver:self];        //移除指定name关联的通知    [[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_CHANGECONTENT object:nil];}//收到通知后,触发的方法,然后做相应的处理-(void)contentChange:(NSNotification *)notification{                                                                                                                            //userInfo : 通知里捆绑的数据    self.detailLab.text = [NSString stringWithFormat:@"“%@” 发来贺电",[notification.userInfo objectForKey:@"ChangeContent"]];}#pragma mark - 用户交互(push第二个控制器UseNotifi_VCTwo)- (IBAction)gotoPostNotifiVC:(id)sender {        UseNotifi_VCTwo *vc = [[UseNotifi_VCTwo alloc] init];        [self.navigationController pushViewController:vc animated:YES];    }
#import <UIKit/UIKit.h>/* 通知的发送者     xxxxxxxxxxxxxxxxxxxxxxxxxx *///定义 通知的 标识,根据需要选择将 #define 定义在具体的文件中,如.pch、.h等#define  NOTIFICATION_CHANGECONTENT @"Notification_ChangeContent"//通知的标识@interface UseNotifi_VCTwo : UIViewController@end

第二个UIviewController( UseNotifi_VCTwo.m)

@interface UseNotifi_VCTwo ()@property (weak, nonatomic) IBOutlet UITextField *nameTF;@end@implementation UseNotifi_VCTwo- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization        self.title = @"发送通知的控制器";    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}#pragma mark - 用户交互(post回去)- (IBAction)postNotificationAction:(id)sender {        NSString *uname = self.nameTF.text;    NSDictionary *ChangeContent = [NSDictionary dictionaryWithObject:uname forKey:@"ChangeContent"];        //发送一个通知    /*     [NSNotificationCenter defaultCenter]  获取全局通知对象     postNotificationName: 通知的标识,必须设置     object: 用于通知的过滤,将通知捆绑一个obj,一般设为nil     userInfo: 发送通知所捆绑的用户数据     */     [[NSNotificationCenter defaultCenter]     postNotificationName:NOTIFICATION_CHANGECONTENT                   object:nil                 userInfo:ChangeContent];        [self.navigationController popViewControllerAnimated:YES];}@end

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×