这个工程创建一个根视图控制器,加载在widow上。并且在其视图上添加一个label标签和button按钮,label标签text设置为helloworld,当单击button时,弹一个模态视图,模态视图中包含一个返回按钮,返回根视图时,将label标签的值设定为textField的值,用通知方法实现。
#pragma ----AppDelegate.h-----
#import <UIKit/UIKit.h>
@class RootViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (retain,nonatomic) RootViewController *myRootvc;
@end
#pragma ----AppDelegate.m-----
#import "AppDelegate.h"
#import "RootViewController.h"
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[_myRootvc release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
RootViewController *tempMyRootVC = [[RootViewController alloc]init];
self.myRootvc = tempMyRootVC;
[tempMyRootVC release];
self.window.rootViewController = self.myRootvc;
[self.window makeKeyAndVisible];
return YES;
}
@end
#pragma ----RootViewController.h----
#import <UIKit/UIKit.h>
@class ModalViewController;
@interface RootViewController : UIViewController
@end
#pragma ----RootViewController.m----
#import "RootViewController.h"
#import "ModalViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (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 purpleColor];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(90, 200, 150, 40);
[myButton setTitle:@"Present" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(presentModalVC) forControlEvents:UIControlEventTouchUpInside];
myButton.backgroundColor = [UIColor redColor];
[self.view addSubview:myButton];
UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 150, 140, 40)];
tempLabel.text = @"Hello world";
tempLabel.tag = 102;
tempLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:tempLabel];
[tempLabel release];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabelText:) name:@"ChangeLabelTextNotification" object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)presentModalVC
{
ModalViewController *myModalVC = [[ModalViewController alloc]init];
myModalVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:myModalVC animated:YES completion:^{
NSLog(@"callback") ;
}];
}
-(void)changeLabelText:(NSNotification*)notification
{
id text = notification.object;
UILabel *label = (UILabel*)[self.view viewWithTag:102];
label.text = text;
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"ChangeLabelTextNotification" object:nil];
[super dealloc];
}
@end
#pragma ----ModalViewController.h---
#import <UIKit/UIKit.h>
@protocol ModalViewControllerDelegate <NSObject>
@optional
-(void)changeLabelText:(NSString*)text;
@end
@interface ModalViewController : UIViewController
{
@private
UITextField *_myTextField;
}
@end
#pragma ----ModalViewController.m---
#import "ModalViewController.h"
@interface ModalViewController ()
@end
@implementation ModalViewController
-(void)dealloc
{
[_myTextField 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 blueColor];
_myTextField = [[UITextField alloc]initWithFrame:CGRectMake(70, 200, 180, 40)];
[self.view addSubview:_myTextField];
_myTextField.borderStyle = UITextBorderStyleRoundedRect;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(90, 150, 140, 40);
[myButton setTitle:@"dismiss" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
myButton.backgroundColor = [UIColor redColor];
[self.view addSubview:myButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dismiss
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeLabelTextNotification" object:_myTextField.text];
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"dismiss");
}];
}
@end