iOS编程-------UINavigationController / 界面间通信(传值)

//
//  AppDelegate.h
//  UI_08UINavigationControl_jiemianchuanzhi
//
//  Created by l on 15/9/10.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end




//
//  AppDelegate.m
//  UI_08UINavigationControl_jiemianchuanzhi
//
//  Created by l on 15/9/10.
//  Copyright (c) 2015年 . All rights reserved.
//

#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];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];




    //一. UINavigationController
     RootViewController *rootVC = [[RootViewController alloc] init];
    //注意: 导航控制器的特点:1.控制具有层次关系的控制器
    //  2.创建的时候要有一个栈底(根视图)控制器

    //1.创建导航控制器
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootVC];


    //设置导航控制器的属性

    //UINavigationBar 属于view层,显示层

    //导航条样式
//    navigationController.navigationBar.barStyle = UIBarStyleBlack;//默认的样式白背景 黑标题,black黑背景 白标题
//    navigationController.navigationBar.translucent = NO;//设置导航条是否透明

//    navigationController.navigationBar.tintColor = [UIColor redColor];//导航条上控件的颜色
//    navigationController.navigationBar.barTintColor = [UIColor yellowColor];//导航条背景颜色

   //***给导航条添加背景图
    [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"login_header.jpg"] forBarMetrics:(UIBarMetricsDefault)];

    //导航控制器 navigationItem 是要添加到navigationBar上面的数据,导航控制器的导航条,显示的是栈里面控制器的navigationItem



    //2.把导航控制器设置为window的根视图控制器
    self.window.rootViewController = navigationController;

    //3.释放
    [rootVC release];
    [navigationController release];

  return YES;
}

@end





//




//
//  RootViewController.h
//  UI_08UINavigationControl_jiemianchuanzhi
//
//  Created by l on 15/9/10.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
//label
@property (nonatomic, retain, readonly) UILabel *label;

@property (nonatomic, retain, readonly) NSString *labelText;

@end




//
//  RootViewController.m
//  UI_08UINavigationControl_jiemianchuanzhi
//
//  Created by l on 15/9/10.
//  Copyright (c) 2015年 . All rights reserved.
//

#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{
  //取到第二个页面的textField.text 并且赋值给自己的label.text
    _label.text = text;
}


- (void)viewDidLoad {


    //textField
    _textField = [[UITextField alloc] initWithFrame:(CGRectMake(100, 250, 200, 40))];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.placeholder = @"请输入";
    [self.view addSubview:_textField];

    //label 展示第二个界面传递过来的值
    _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 = @"爷爷";

    //--------------------------------

   //创建一个button按钮,点击进入下一级页面
    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];

    //---------------------------------

    //navigationItem 的相关属性
    //navigationItem 要赋值给到子控制器
    //1.title 标题
    self.navigationItem.title = @"首页";

    //2.titleView标题视图
//    UIView *titleView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 100, 44))];
//    titleView.backgroundColor = [UIColor yellowColor];
//    self.navigationItem.titleView = titleView;


    //3. BarButtonItem
    //1.创建barButtonItem
//    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemCamera) target:self action:@selector(leftBarButtonAction:)];


    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"left.png"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBarButtonAction:)];



    //2.赋值给navigation 的barButtonItem 属性
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;

    //3.释放
    [leftBarButtonItem release];


    // rightBarButtonItem
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    [rightBarButtonItem release];

// Do any additional setup after loading the view.
}


//rightBarButtonAction:触发方法
- (void)rightBarButtonAction:(UIBarButtonItem *)barButtonItem{
    NSLog(@"分享");
}

//leftBarButtonAction:触发方法
- (void)leftBarButtonAction:(UIBarButtonItem *)barButtonItem{

    NSLog(@"分享到qq空间");
}


#pragma mark-- presentVC方法
- (void) presentVC:(UIButton *)button{

    //1.创建一个控制器
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    //2.模态显示,从下面显示
    [self presentViewController:secondVC animated:YES completion:nil];
    [secondVC release];

    /*
    音乐播放  (需要导入AVFoundation.framework框架)
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"song.mp3" withExtension:nil];
    AVAudioPlayer *mp3 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [mp3 play];
    [mp3 pause];
    [mp3 stop];
     */

}

#pragma mark-- enterNext方法
- (void)enterNext:(UIButton *)button{

    //进入下一级页面
    //1.创建 出下一级页面对象 secondVC
    SecondViewController *secondVC = [[SecondViewController alloc] init];


    //设置代理(从后往前传)
    secondVC.delegate = self;

    //传值(从前往后传)
    //把rootVC 中的textField 的text传递给secondVC的labelText,然后赋值给secondVC的label
    secondVC.labelText = _textField.text;

    //2.导航控制器把secondVC压入栈
    [self.navigationController pushViewController:secondVC animated:YES];
    //3.释放
    [secondVC release];
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end











//
//  SecondViewController.h
//  UI_08UINavigationControl_jiemianchuanzhi
//
//  Created by l on 15/9/10.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>
@class SecondViewController;
//代理传值
@protocol SecondViewControllerDelegate <NSObject>

//代理执行的方法
//当第二个页面 返回到第一个页面的时候 代理执行该方法
//secondVC 第二个页面
//text 第二个页面textField的text
- (void)secondViewControllerBackFront:(SecondViewController *)secondVC textFieldText:(NSString *)text;

@end

@interface SecondViewController : UIViewController
//label
@property (nonatomic, retain, readonly)UILabel *label;

//string
@property (nonatomic, copy) NSString *labelText;//接受第一个页面的textField传值

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

@end





//
//  SecondViewController.m
//  UI_08UINavigationControl_jiemianchuanzhi
//
//  Created by l on 15/9/10.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "RootViewController.h"
@interface SecondViewController ()
@property (nonatomic, retain)UITextField *textField;
@end

@implementation SecondViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    //textField
    _textField = [[UITextField alloc] initWithFrame:(CGRectMake(100, 260, 200, 50))];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];

    //label
    _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];

    //enterNext 进入下一级按钮
    UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
//    button2.backgroundColor = [UIColor redColor];
    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];



    // Do any additional setup after loading the view.
}


#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{
    //方法一:
//    RootViewController *rootVC = (RootViewController *)self.navigationController.viewControllers[0];
//    rootVC.label.text = _textField.text;



    //方法二:
    if (_delegate != nil && [_delegate respondsToSelector:@selector(secondViewControllerBackFront:textFieldText:)]) {
        //把第二个页面的textField.text通过方法参数传递给第一个页面
        //代理执行方法
        [_delegate secondViewControllerBackFront:self textFieldText:self.textField.text];

    }

    //1.取到navigation 调用popViewController
    [self.navigationController popViewControllerAnimated:YES];

}


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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end




//



//
//  ThirdViewController.h
//  UI_08UINavigationControl_jiemianchuanzhi
//
//  Created by l on 15/9/10.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@end




//
//  ThirdViewController.m
//  UI_08UINavigationControl_jiemianchuanzhi
//
//  Created by l on 15/9/10.
//  Copyright (c) 2015年 . All rights reserved.
//

#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];
    // Do any additional setup after loading the view.
}

- (void)backAction:(UIButton *)button{
    //返回到指定的视图控制器
    //1.
    NSArray *controllers = self.navigationController.viewControllers;
    [self.navigationController popToViewController:controllers[1] animated:YES];
    //返回到根视图控制器
//  [self.navigationController popToRootViewControllerAnimated:YES];
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值