您好,登錄后才能下訂單哦!
通知傳值
第一個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.
//監(jiān)聽某個通知
//這里需要注意到 : 通知只能是 對象才可用,且該對象必須存在于內(nèi)存中
/*
[NSNotificationCenter defaultCenter] 獲取通知的管理
addObserver: 設(shè)置通知的監(jiān)聽者
*/
//NOTIFICATION_CHANHECONTENT 在 QFUseNotifi_VCTwo中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentChange:) name:NOTIFICATION_CHANGECONTENT object:nil];
}
-(void)dealloc
{ // 通知
// |
// \ /
// 對象-->監(jiān)聽<--通知
// 將當(dāng)前對象監(jiān)聽的所有通知移除
[[NSNotificationCenter defaultCenter] removeObserver:self];
//移除指定name關(guān)聯(lián)的通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:NOTIFICATION_CHANGECONTENT object:nil];
}
//收到通知后,觸發(fā)的方法,然后做相應(yīng)的處理
-(void)contentChange:(NSNotification *)notification
{
//userInfo : 通知里捆綁的數(shù)據(jù)
self.detailLab.text = [NSString stringWithFormat:@"“%@” 發(fā)來賀電",[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 <UIKit/UIKit.h>
/*
通知的發(fā)送者 xxxxxxxxxxxxxxxxxxxxxxxxxx
*/
//定義 通知的 標(biāo)識,根據(jù)需要選擇將 #define 定義在具體的文件中,如.pch、.h等
#define NOTIFICATION_CHANGECONTENT @"Notification_ChangeContent"http://通知的標(biāo)識
@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 = @"發(fā)送通知的控制器";
}
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"];
//發(fā)送一個通知
/*
[NSNotificationCenter defaultCenter] 獲取全局通知對象
postNotificationName: 通知的標(biāo)識,必須設(shè)置
object: 用于通知的過濾,將通知捆綁一個obj,一般設(shè)為nil
userInfo: 發(fā)送通知所捆綁的用戶數(shù)據(jù)
*/
[[NSNotificationCenter defaultCenter]
postNotificationName:NOTIFICATION_CHANGECONTENT
object:nil
userInfo:ChangeContent];
[self.navigationController popViewControllerAnimated:YES];
}
@end
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。