网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

iOS开发Notification传值


网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、重庆小程序开发公司、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了徽县免费建站欢迎大家使用!

通知传值


第一个UIViewcontroller.h

#import 

@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];
   
}


第二个UIviewController( UseNotifi_VCTwo.h)

#import 

/*
通知的发送者     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

当前标题:iOS开发Notification传值
转载来于:http://bjjierui.cn/article/ppgoog.html

其他资讯