您好,登錄后才能下訂單哦!
通知傳值
//流程:
1.
注冊通知
2. 通知中心 , 發送一條消息通知 ---------- 其中 name 名字千萬不要寫錯了 , 會出現在3 個地方 3. 實現通知中心內部的方法 , 并實現傳值
4. 第四步 , 消息發送完 , 要移除掉
代碼如下:
#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
#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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。