ModalViewController.h文件
#import <UIKit/UIKit.h>
@protocol ModalViewControllerDelegate <NSObject>
-(void)changeLabelText:(NSString *)text;
@end
@interface ModalViewController : UIViewController{
@private
UITextField * _field;
}
@property (nonatomic,assign) id <ModalViewControllerDelegate> delegate;
@end
ModalViewController.m
@synthesize delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view. backgroundColor = [UIColor purpleColor];
_field = [[UITextField alloc]initWithFrame:CGRectMake(320/2 - 120/2, 100, 120, 40)];
_field.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_field];
[_field release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(320/2 - 120/2, 150, 120, 40);
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self
action:@selector(goToBack)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)goToBack{
//委托(代理)机制实现
// if([self.delegate respondsToSelector:@selector(changeLabelText:)]){
// [self.delegate changeLabelText:_field.text];
// }
//消息机制实现
[[NSNotificationCenter defaultCenter] postNotificationName:kChangeLabelTextNotification object:_field.text];
if([[UIDevice currentDevice].systemVersion floatValue] < 6.0){
[self dismissModalViewControllerAnimated:YES];
}else{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"back");
}];
}
}
XViewController.h
#import <UIKit/UIKit.h>
#import "ModalViewController.h"
@interface XViewController : UIViewController<ModalViewControllerDelegate>
@end
XViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(320/2 - 120/2, 100, 120, 30)];
label.text = @"Hello World";
label.tag = 1;
label.textAlignment = UITextAlignmentCenter;
[self.view addSubview:label];
[label release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(320/2 - 120/2, 150, 120, 30);
[button setTitle:@"跳转" forState:UIControlStateNormal];
[button addTarget:self
action:@selector(goTo)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//消息机制实现
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector
(changeLabelText:) name:kChangeLabelTextNotification object:nil];
}
-(void)goTo{
ModalViewController *modalVC = [[ModalViewController alloc]init];
//委托(代理)机制实现
// modalVC.delegate = self;
modalVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
if([[UIDevice currentDevice].systemVersion floatValue] < 6.0){
[self presentModalViewController:modalVC animated:YES];
}
}
//委托(代理)机制实现
/*-(void)changeLabelText:(NSString *)text{
UILabel *label = (UILabel*)[self.view viewWithTag:1];
label.text = text;
}*/
//消息机制实现
-(void)changeLabelText:(NSNotification *)notification{
id text = notification.object;
UILabel *label = (UILabel*)[self.view viewWithTag:1];
label.text = text;
}
MyTestThree-Prefix.pch
#define kChangeLabelTextNotification @"ChangeLabelTextNotification"
需要注意的是:
1、
textField.borderStyle = UITextBorderStyleRoundedRect;
不设置的话,默认是不可见的
2、
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
这里没有使用道alloc,retain,copy等,因此不能使用release
3、
ModalViewController *modalVC = [[ModalViewController alloc]init];
不能去release掉,因为后来会要用到,因此最好的是将其放在.h文件中