文件

#import "OneAppDelegate.h"
#import "OneViewController.h"
@implementation

导航控制器的代理方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSLog(@"已经显示:%@", viewController);
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSLog(@"即将显示:%@", viewController);
}

- (void)dealloc
{
_windowrelease];
superdealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
{
self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]]autorelease];
// Override point for customization after application launch.
//初始化一个根控制器
OneViewController *root = [[[OneViewControlleralloc] init] autorelease];

UINavigationController *vc = [[[UINavigationControlleralloc] initWithRootViewController:root]autorelease];

delegate =self;

self.window.rootViewController = vc;

[self.windowmakeKeyAndVisible];
return YES;
}


OneViewController.h文件

#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate;

@interface OneViewController :UIViewController <SecondViewControllerDelegate>
- (void)setString:(NSString
@end



OneViewController.m文件

#import "OneViewController.h"
#import "SecondViewController.h"
@interface OneViewController ()

@end

@implementation

的方法
- (void)setSecondData:(NSString
NSLog(@"从Second传过来的数据:%@", str);
}

其他方法
- (void)setString:(NSString
NSLog(@"从Second传过来的数据:%@", str);
}

- (void)left {
NSLog(@"点击了左边");
}

- (void)right {
SecondViewController *second = [[SecondViewControlleralloc] init];
title =@"第2个控制器";

delegate =self;

//传递数据给下一个控制器
setString:@“。。。。。。。”];

// 将second放入栈中
// self.navigationController可以拿到子控制器所在的导航控制器
[self.navigationControllerpushViewController:second animated:YES];

release];
}

- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title =@"红色控制器";
//self.navigationItem.title = @"红色控制器";

// 设置左边的item
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(left)]autorelease];

// 设置右边的item
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeContactAdd];
[btn addTarget:selfaction:@selector(right)forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItemalloc] initWithCustomView:btn]autorelease];

self.view.backgroundColor = [UIColorredColor];

//设置返回键的文字为"返回"
self.navigationItem.backBarButtonItem = [[[UIBarButtonItemalloc] initWithTitle:@"返回"style:UIBarButtonItemStylePlaintarget:nilaction:nil]autorelease];
}

- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end



SecondViewController.h文件

#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate;

//@class OneViewController;
@interface SecondViewController :UIViewController

//@property (nonatomic, retain) OneViewController *One;

- (void)setString:(NSString

- (IBAction)click:(id)sender;

@property (nonatomic,retain) id<SecondViewControllerDelegate> delegate;
@end

@protocol SecondViewControllerDelegate <NSObject>
- (void)setSecondData:(NSString
@end


文件


#import "SecondViewController.h"
//#import "OneViewController.h"

@interface SecondViewController ()

@end

@implementation


- (void)viewDidLoad
{
[superviewDidLoad];

}

- (void)viewDidUnload {
[superviewDidUnload];
self.delegate =nil;
//self.One = nil;
}

- (void)dealloc {
[_delegate release];
//[_One release];
superdealloc];
}

公共方法
- (void)setString:(NSString
NSLog(@"从One传过来的数据:%@", string);
}

- (void)click:(id)sender {
//一口气跳回根控制器(会把中间的控制器都移到栈外)
// [self.navigationController popToRootViewControllerAnimated:YES];

//跳回到指定的控制器
// [self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];

//[self.One setString:@"12345"];

if ([self.delegaterespondsToSelector:@selector(setSecondData:)]) {
self.delegatesetSecondData:@"12345"];
}

//移除栈顶的控制器
[self.navigationControllerpopViewControllerAnimated:YES];
}
@end