OneAppDelegate.m文件
#import "OneAppDelegate.h"
#import "OneViewController.h"
@implementation OneAppDelegate
#pragma mark 導航控制器的代理方法
- (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 *)launchOptions
{
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];
vc.delegate =self;
self.window.rootViewController = vc;
[self.windowmakeKeyAndVisible];
return YES;
}
OneViewController.h文件
#import
@protocol SecondViewControllerDelegate;
@interface OneViewController :UIViewController
- (void)setString:(NSString *)str;
@end
OneViewController.m文件
#import "OneViewController.h"
#import "SecondViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
#pragma mark - SecondViewControllerDelegate的方法
- (void)setSecondData:(NSString *)str {
NSLog(@"從Second傳過來的數據:%@", str);
}
#pragma mark - 其他方法
- (void)setString:(NSString *)str {
NSLog(@"從Second傳過來的數據:%@", str);
}
- (void)left {
NSLog(@"點擊了左邊");
}
- (void)right {
SecondViewController *second = [[SecondViewControlleralloc] init];
second.title =@"第2個控制器";
second.delegate =self;
//傳遞數據給下一個控制器
[secondsetString:@“。。。。。。。”];
// 將second放入棧中
// self.navigationController可以拿到子控制器所在的導航控制器
[self.navigationControllerpushViewController:second animated:YES];
[secondrelease];
}
- (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
@protocol SecondViewControllerDelegate;
//@class OneViewController;
@interface SecondViewController :UIViewController
//@property (nonatomic, retain) OneViewController *One;
- (void)setString:(NSString *)string;
- (IBAction)click:(id)sender;
@property (nonatomic,retain) id
@end
@protocol SecondViewControllerDelegate
- (void)setSecondData:(NSString *)str;
@end
SecondViewController.m文件
#import "SecondViewController.h"
//#import "OneViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad
{
[superviewDidLoad];
}
- (void)viewDidUnload {
[superviewDidUnload];
self.delegate =nil;
//self.One = nil;
}
- (void)dealloc {
[_delegate release];
//[_One release];
[superdealloc];
}
#pragma mark - 公共方法
- (void)setString:(NSString *)string {
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