//使用通知进行消息传递(通知NSNotificationCenter不可都用)
//NSNotificationCenter可以在毫无关系的视图中进行参数传值
//NSNotificationCenter是一个one-to-one &one-to-many &one-to- none的关系
//过多使用会降低代码的可读性,增加程序的耦合度
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
//实例化一个用于显示的Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 64, 100, 60)];
label.text = @"没变化";
label.backgroundColor = [UIColor whiteColor];
label.tag = 100;
[self.view addSubview:label];
RELEASE_SAFETY(label);
//实例化一个触发事件按钮
UIButton *presentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect ];
presentButton.backgroundColor = [UIColor whiteColor];
presentButton.frame = CGRectMake(50, 200, 200, 60);
[presentButton setTitle:@"ModalViewController" forState:UIControlStateNormal];
[presentButton addTarget:self
//NSNotificationCenter可以在毫无关系的视图中进行参数传值
//NSNotificationCenter是一个one-to-one &one-to-many &one-to- none的关系
//过多使用会降低代码的可读性,增加程序的耦合度
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
//实例化一个用于显示的Label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 64, 100, 60)];
label.text = @"没变化";
label.backgroundColor = [UIColor whiteColor];
label.tag = 100;
[self.view addSubview:label];
RELEASE_SAFETY(label);
//实例化一个触发事件按钮
UIButton *presentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect ];
presentButton.backgroundColor = [UIColor whiteColor];
presentButton.frame = CGRectMake(50, 200, 200, 60);
[presentButton setTitle:@"ModalViewController" forState:UIControlStateNormal];
[presentButton addTarget:self
action:@selector(presetAction)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:presentButton];
//使用通知进行消息传递(通知NSNotificationCenter不可都用)
//NSNotificationCenter可以在毫无关系的视图中进行参数传值
//NSNotificationCenter是一个one-to-one &one-to-many &one-to- none的关系
//过多使用会降低代码的可读性,增加程序的耦合度
//#define kChangeLabelValueByNotification @"kChangeLabelValueByNotification"宏定义
[[NSNotificationCenter defaultCenter] addObserver:self
[self.view addSubview:presentButton];
//使用通知进行消息传递(通知NSNotificationCenter不可都用)
//NSNotificationCenter可以在毫无关系的视图中进行参数传值
//NSNotificationCenter是一个one-to-one &one-to-many &one-to- none的关系
//过多使用会降低代码的可读性,增加程序的耦合度
//#define kChangeLabelValueByNotification @"kChangeLabelValueByNotification"宏定义
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeLabelText:)
name:kChangeLabelValueByNotification
object:nil];
}
#pragma mark==实现通知方法=
-(void)changeLabelText:(NSNotification *)notification{
//获取通知传递过来的对象
id label = notification.object;
//修改原有Label值
((UILabel *)[self.view viewWithTag:100]).text = label;
}
//实现模态方法
-(void)presetAction{
ModalViewController *modal = [[ModalViewController alloc] init];
[self presentViewController:modal animated:YES completion:nil];
RELEASE_SAFETY(modal);
}
//重写析构函数
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self
}
#pragma mark==实现通知方法=
-(void)changeLabelText:(NSNotification *)notification{
//获取通知传递过来的对象
id label = notification.object;
//修改原有Label值
((UILabel *)[self.view viewWithTag:100]).text = label;
}
//实现模态方法
-(void)presetAction{
ModalViewController *modal = [[ModalViewController alloc] init];
[self presentViewController:modal animated:YES completion:nil];
RELEASE_SAFETY(modal);
}
//重写析构函数
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:kChangeLabelValueByNotification
object:nil];
[super dealloc];
}
[super dealloc];
}
//ModalViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
//实例化一个用于显示的Label
self.textField = [[[UITextField alloc] initWithFrame:CGRectMake(50, 64, 100, 60)] autorelease];
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.textField];
//实例化一个触发事件按钮
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect ];
dismissButton.backgroundColor = [UIColor whiteColor];
dismissButton.frame = CGRectMake(50, 200, 200, 60);
[dismissButton setTitle:@"dismissViewController" forState:UIControlStateNormal];
[dismissButton addTarget:self
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
//实例化一个用于显示的Label
self.textField = [[[UITextField alloc] initWithFrame:CGRectMake(50, 64, 100, 60)] autorelease];
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.textField];
//实例化一个触发事件按钮
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect ];
dismissButton.backgroundColor = [UIColor whiteColor];
dismissButton.frame = CGRectMake(50, 200, 200, 60);
[dismissButton setTitle:@"dismissViewController" forState:UIControlStateNormal];
[dismissButton addTarget:self
action:@selector(dismissAction)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dismissButton];
}
-(void)dismissAction{
[[NSNotificationCenter defaultCenter] postNotificationName:kChangeLabelValueByNotification
[self.view addSubview:dismissButton];
}
-(void)dismissAction{
[[NSNotificationCenter defaultCenter] postNotificationName:kChangeLabelValueByNotification
object:self.textField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)dealloc{
RELEASE_SAFETY(_textField);
[super dealloc];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)dealloc{
RELEASE_SAFETY(_textField);
[super dealloc];
}