溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS 開發 Notification傳值

發布時間:2020-09-21 09:15:10 來源:網絡 閱讀:601 作者:xyjn332 欄目:移動開發


通知傳值


第一個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];
   
}


第二個UIviewController( UseNotifi_VCTwo.h)

#import <UIKit/UIKit.h>

/*
通知的發送者     xxxxxxxxxxxxxxxxxxxxxxxxxx
*/

//定義 通知的 標識,根據需要選擇將 #define 定義在具體的文件中,如.pch、.h等
#define  NOTIFICATION_CHANGECONTENT @"Notification_ChangeContent"http://通知的標識

@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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女