#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootVC];
[navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"login_header.jpg"] forBarMetrics:(UIBarMetricsDefault)];
self.window.rootViewController = navigationController;
[rootVC release];
[navigationController release];
return YES;
}
@end
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@property (nonatomic, retain, readonly) UILabel *label;
@property (nonatomic, retain, readonly) NSString *labelText;
@end
#import "RootViewController.h"
#import "SecondViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface RootViewController ()<SecondViewControllerDelegate>//遵守协议
@property (nonatomic, retain) UITextField *textField;
@end
@implementation RootViewController
- (void)dealloc{
[_textField release];
[_label release];
[super dealloc];
}
- (void)secondViewControllerBackFront:(SecondViewController *)secondVC textFieldText:(NSString *)text{
_label.text = text;
}
- (void)viewDidLoad {
_textField = [[UITextField alloc] initWithFrame:(CGRectMake(100, 250, 200, 40))];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.placeholder = @"请输入";
[self.view addSubview:_textField];
_label = [[UILabel alloc] initWithFrame:(CGRectMake(100, 300, 200, 40))];
_label.backgroundColor = [UIColor grayColor];
[self.view addSubview:_label];
[super viewDidLoad];
self.view.backgroundColor = [UIColor magentaColor];
self.title = @"爷爷";
UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
button.frame = CGRectMake(100, 100, 100, 50);
button.titleLabel.font = [UIFont systemFontOfSize:18];
[button setTitle:@"下一级" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(enterNext:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
button1.frame = CGRectMake(100, 200, 200, 50);
button1.titleLabel.font = [UIFont systemFontOfSize:18];
[button1 setTitle:@"模态控制器" forState:(UIControlStateNormal)];
[button1 addTarget:self action:@selector(presentVC:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button1];
self.navigationItem.title = @"首页";
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBarButtonAction:)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
[leftBarButtonItem release];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBarButtonAction:)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
[rightBarButtonItem release];
}
- (void)rightBarButtonAction:(UIBarButtonItem *)barButtonItem{
NSLog(@"分享");
}
- (void)leftBarButtonAction:(UIBarButtonItem *)barButtonItem{
NSLog(@"分享到qq空间");
}
#pragma mark-- presentVC方法
- (void) presentVC:(UIButton *)button{
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];
[secondVC release];
}
#pragma mark-- enterNext方法
- (void)enterNext:(UIButton *)button{
SecondViewController *secondVC = [[SecondViewController alloc] init];
secondVC.delegate = self;
secondVC.labelText = _textField.text;
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@class SecondViewController;
@protocol SecondViewControllerDelegate <NSObject>
- (void)secondViewControllerBackFront:(SecondViewController *)secondVC textFieldText:(NSString *)text;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, retain, readonly)UILabel *label;
@property (nonatomic, copy) NSString *labelText;
@property (nonatomic, assign) id<SecondViewControllerDelegate> delegate;
@end
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "RootViewController.h"
@interface SecondViewController ()
@property (nonatomic, retain)UITextField *textField;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
_textField = [[UITextField alloc] initWithFrame:(CGRectMake(100, 260, 200, 50))];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
_label = [[UILabel alloc] initWithFrame:(CGRectMake(100, 310, 200, 50))];
_label.backgroundColor = [UIColor grayColor];
_label.text = _labelText;
[self.view addSubview:_label];
self.view.backgroundColor = [UIColor cyanColor];
self.title = @"爸爸";
UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
button.frame = CGRectMake(100, 50, 100, 50);
button.backgroundColor = [UIColor grayColor];
[button setTitle:@"上一级" forState:(UIControlStateNormal)];
button.titleLabel.font = [UIFont systemFontOfSize:18];
[button addTarget:self action:@selector(backFront:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
button2.frame = CGRectMake(100, 120, 100, 50);
[button2 setTitle:@"下一级" forState:(UIControlStateNormal)];
button2.titleLabel.font = [UIFont systemFontOfSize:18];
[button2 addTarget:self action:@selector(enterNext:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
button3.frame = CGRectMake(100, 200, 100, 50);
button3.titleLabel.font = [UIFont systemFontOfSize:18];
[button3 setTitle:@"模态消失" forState:(UIControlStateNormal)];
[button3 addTarget:self action:@selector(dismiss:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button3];
}
#pragma mark-- dismiss方法
- (void)dismiss:(UIButton *)button{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark-- enterNext方法
- (void)enterNext:(UIButton *)button{
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirdVC animated:YES];
[thirdVC release];
}
#pragma mark-- backFront方法
- (void)backFront:(UIButton *)button{
if (_delegate != nil && [_delegate respondsToSelector:@selector(secondViewControllerBackFront:textFieldText:)]) {
[_delegate secondViewControllerBackFront:self textFieldText:self.textField.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController
@end
#import "ThirdViewController.h"
#import "RootViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"儿子";
self.view.backgroundColor = [UIColor greenColor];
UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
button.titleLabel.font = [UIFont systemFontOfSize:18];
button.frame = CGRectMake(100, 100, 100, 50);
[button setTitle:@"返回" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
- (void)backAction:(UIButton *)button{
NSArray *controllers = self.navigationController.viewControllers;
[self.navigationController popToViewController:controllers[1] animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end