日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通知传值(NSNotificationCenter)

發布時間:2025/4/16 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通知传值(NSNotificationCenter) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?315人閱讀?評論(0)?收藏?舉報

NSNotificationCenteruitableviewcellNSNotificationios開發界面


通知傳值

//流程:
1.
注冊通知
2.
通知中心,發送一條消息通知----------其中name名字千萬不要寫錯了,會出現在3個地方
3.
實現通知中心內部的方法,并實現傳值
4.
第四步,消息發送完,要移除掉

代碼如下:

[objc]?view plaincopy

  • #import?"FirstViewController.h"??

  • #import?"SecondViewController.h"??

  • #import?"UIButton+Create.h"??

  • @interface?FirstViewController?()??

  • {??

  • ????UILabel?*?_label;??

  • }??

  • @end??

  • ??

  • @implementation?FirstViewController??

  • -?(void)dealloc??

  • {??

  • ????[_label?release];??

  • ????//第四步,消息發送完,要移除掉??

  • ????[[NSNotificationCenter?defaultCenter]removeObserver:self?name:@"labelTextNotification"?object:nil];??

  • ????[super?dealloc];??

  • }??

  • -?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil??

  • {??

  • ????self?=?[super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];??

  • ????if?(self)?{??

  • ????????//?Custom?initialization??

  • ????}??

  • ????return?self;??

  • }??

  • ??

  • -?(void)viewDidLoad??

  • {??

  • ????[super?viewDidLoad];??

  • ??????

  • ??????

  • ????self.view.backgroundColor?=?[UIColor?redColor];??

  • ????self.navigationItem.title?=?@"首頁";??

  • ??????

  • ????_label?=?[[UILabel?alloc]initWithFrame:CGRectMake(50,?80,?200,?30)];??

  • ????_label.backgroundColor?=?[UIColor?greenColor];??

  • ????[self.view?addSubview:_label];??

  • ??????

  • ??????

  • ??????

  • ????UIButton?*?button?=?[UIButton?systemButtonWithFrame:CGRectMake(100,?120,?50,?50)?title:@"Push"?target:self?action:@selector(didClickButtonAction)];??

  • ????[self.view?addSubview:button];??

  • ??????

  • ????//第二步,通知中心,發送一條消息通知----------其中name名字千萬不要寫錯了,會出現在3個地方??

  • ????[[NSNotificationCenter?defaultCenter]addObserver:self?selector:@selector(showLabelText:)?name:@"labelTextNotification"?object:nil];??

  • ??????

  • ???

  • ??????

  • ??????

  • ????//?Do?any?additional?setup?after?loading?the?view.??

  • }??

  • ??

  • -?(void)showLabelText:(NSNotification?*)notification??

  • {??

  • ????//第三,實現通知中心內部的方法,并實現傳值??

  • ????id?text?=?notification.object;??

  • ????_label.text?=?text;??

  • }??

  • ??

  • -?(void)didClickButtonAction??

  • {??

  • ??????

  • ????

  • ????SecondViewController?*?secondVC?=?[[SecondViewController?alloc]init];??

  • ????[self.navigationController?pushViewController:secondVC?animated:YES];??

  • ????[secondVC?release];??

  • }??

  • ??

  • -?(void)didClick:(NSString?*)text??

  • {??

  • ????_label.text?=?text;??

  • }??

  • ??

  • -?(void)didReceiveMemoryWarning??

  • {??

  • ????[super?didReceiveMemoryWarning];??

  • ????//?Dispose?of?any?resources?that?can?be?recreated.??

  • }??

  • ??

  • @end??




  • [objc]?view plaincopy

  • #import?"SecondViewController.h"??

  • #import?"FirstViewController.h"??

  • #import?"UIButton+Create.h"??

  • @interface?SecondViewController?()??

  • {??

  • ????UITextField?*?_textField;//創建一個輸入框??

  • }??

  • @end??

  • @implementation?SecondViewController??

  • ??

  • -?(void)dealloc??

  • {??

  • ????[_textField?release];??

  • ????[super?dealloc];??

  • }??

  • -?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil??

  • {??

  • ????self?=?[super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];??

  • ????if?(self)?{??

  • ????????//?Custom?initialization??

  • ????}??

  • ????return?self;??

  • }??

  • ??

  • -?(void)viewDidLoad??

  • {??

  • ????[super?viewDidLoad];??

  • ????self.view.backgroundColor?=?[UIColor?orangeColor];??

  • ????self.navigationItem.title?=?@"第二頁";??

  • ????

  • ??????

  • ??????

  • ????_textField?=?[[UITextField?alloc]initWithFrame:CGRectMake(50,?80,?200,?30)];??

  • ????_textField.borderStyle?=?UITextBorderStyleRoundedRect;??

  • ????[self.view?addSubview:_textField];??

  • ??????

  • ??????

  • ??????

  • ??????

  • ????UIButton?*?button?=?[UIButton?systemButtonWithFrame:CGRectMake(100,?120,?50,?50)?title:@"Back"?target:self?action:@selector(didClickButtonAction)];??

  • ????[self.view?addSubview:button];??

  • ???

  • ??????

  • ??????

  • ??????

  • ??????

  • ????//?Do?any?additional?setup?after?loading?the?view.??

  • }??

  • ??

  • -?(void)didClickButtonAction??

  • {??

  • ??????

  • ????//第一步注冊通知??

  • ????[[NSNotificationCenter?defaultCenter]postNotificationName:@"labelTextNotification"?object:_textField.text];??

  • ????[self.navigationController?popToRootViewControllerAnimated:YES];??

  • }??

  • ??

  • ??

  • ??

  • -?(void)didReceiveMemoryWarning??

  • {??

  • ????[super?didReceiveMemoryWarning];??

  • ????//?Dispose?of?any?resources?that?can?be?recreated.??

  • }??

  • ??

  • @end??






  • 轉載于:https://blog.51cto.com/9453125/1561046

    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的通知传值(NSNotificationCenter)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。