这个工程创建一个根视图控制器,加载在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>
#import "ModalViewController.h"
@interface RootViewController : UIViewController<ModalViewControllerDelegate>
@end
#pragma ----RootViewController.m-----
#import "RootViewController.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(110, 200, 100, 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];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)presentModalVC
{
ModalViewController *myModalVC = [[ModalViewController alloc]init];
//一定要设置代理
myModalVC.delegate = self;
myModalVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:myModalVC animated:YES completion:^{
NSLog(@"callback") ;
}];
}
-(void)changeLabelText:(NSString*)text
{
UILabel *label = (UILabel*)[self.view viewWithTag:102];
label.text = text;
}
@end
#pragma ----ModalViewController.h-----
#import <UIKit/UIKit.h>
@protocol ModalViewControllerDelegate <NSObject>
@optional
-(void)changeLabelText:(NSString*)text;
@end
@interface ModalViewController : UIViewController
{
@private
UITextField *_myTextField;
}
@property (nonatomic,assign) id<ModalViewControllerDelegate> delegate;
@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, 190, 180, 40)];
[self.view addSubview:_myTextField];
_myTextField.borderStyle = UITextBorderStyleRoundedRect;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(90, 150, 140, 30);
[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
{
if ([self.delegate respondsToSelector:@selector(changeLabelText:)])
{
[self.delegate changeLabelText:_myTextField.text];
}
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"dismiss");
}];
}
@end