关闭

IOS之【UINavigationController的使用】

标签: uiviewcontrollerdelegateiosobjective-c
984人阅读 评论(0) 收藏 举报
分类:

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 <UIKit/UIKit.h>

@protocol SecondViewControllerDelegate;


@interface OneViewController :UIViewController <SecondViewControllerDelegate>

- (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 <UIKit/UIKit.h>

@protocol SecondViewControllerDelegate;


//@class OneViewController;

@interface SecondViewController :UIViewController


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


- (void)setString:(NSString *)string;


- (IBAction)click:(id)sender;


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

@end


@protocol SecondViewControllerDelegate <NSObject>

- (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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:631105次
    • 积分:9406
    • 等级:
    • 排名:第1998名
    • 原创:272篇
    • 转载:235篇
    • 译文:14篇
    • 评论:31条
    博客专栏
    最新评论