首页 > 代码库 > iOS UI07_导航视图控制器

iOS UI07_导航视图控制器

//
//  MainViewController.m
//  UI07_导航视图控制器
//
//  Created by dllo on 15/8/6.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

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

@property(nonatomic,retain)UITextField *textfield;



@end

@implementation MainViewController
-(void)dealloc
{
    [_textfield release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];
    //导航视图控制器高度44,上面的状态栏是20,加在一起默认是64
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(200, 100, 100, 40);
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    button.layer.borderWidth=1;
    button.layer.cornerRadius=10;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(click:) forControlEvents:
     UIControlEventTouchUpInside];
    //对导航视图控制器进行设置
    //加上一个标题
//    self.title =@"猫眼电影";
    //外观设置
    //背景颜色,不是全部的背景颜色都是backgroundColor
//    self.navigationController.navigationBar.barTintColor=[UIColor greenColor];
    //创建一个UIview
    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view.backgroundColor=[UIColor blackColor];
    [self.view addSubview:view];
    [view release];
    //为了防止坐标系被篡改,我们把bar从半透明设置成不透明,这样坐标系的原点会自己主动向下推64
    self.navigationController.navigationBar.translucent=NO;
    //设置里面的内容
    //标题设置
    self.navigationItem.title=@"鹰王电影";
    //指定一些视图,称为titleView
    UISegmentedControl *seg=[[UISegmentedControl alloc] initWithItems:@[@"信息",@"通话"]];
    self.navigationItem.titleView=seg;
    //创建左右两边的按钮
    self.navigationItem.leftBarButtonItem=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leftButtonAction:)] autorelease];
//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"天平.png"] style:UIBarButtonItemStylePlain target:self action:@selector(right:)];

    //创建一个小button
    UIButton *rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame=CGRectMake(0, 0, 40, 40);
    [rightButton setImage:[UIImage imageNamed:@"天平.png"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:rightButton];

    self.textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 40)];
    self.textfield.layer.borderWidth=1;
    self.textfield.layer.cornerRadius=10;
    [self.view addSubview:self.textfield];
    [self.textfield release];

}
-(void)leftButtonAction:(UIBarButtonItem *)button
{

}
-(void)right:(UIBarButtonItem *)button
{

}


-(void)click:(UIButton *)button
{
//    //用模态跳转下一页
//    SecondViewController *secondVC=[[SecondViewController alloc] init];
//    [secondVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
//    [self presentViewController:secondVC animated:YES completion:^{
//        
//    }];
    //用导航视图控制器Navigation进行跳转
    //先创建下一页对象
    SecondViewController *secVC=[[SecondViewController alloc] init];
    //属性传值第二步
    secVC.number=100;

    secVC.str=self.textfield.text;

    secVC.arr=@[@"杨林",@"刘山山"];
    //
    [self.navigationController pushViewController:secVC animated:YES];
    //内存管理
    [secVC 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
//  UI07_导航视图控制器
//
//  Created by dllo on 15/8/6.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
//属性传值第一步,在第二个页面写一条属性
@property(nonatomic,assign)NSInteger number;
//针对字符串写一条属性
@property(nonatomic,copy)NSString *str;

@property(nonatomic,retain)NSArray *arr;

@end
//
//  SecondViewController.m
//  UI07_导航视图控制器
//
//  Created by dllo on 15/8/6.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@property(nonatomic,retain)UILabel *label;
@end

@implementation SecondViewController
-(void)dealloc
{
    [_label release];
    [super dealloc];
    [_arr release];
    [_str release];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor orangeColor];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(200, 100, 100, 40);
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    button.layer.borderWidth=1;
    button.layer.cornerRadius=10;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(click:) forControlEvents:
     UIControlEventTouchUpInside];

    NSLog(@"%ld",self.number);

    self.label=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 50, 40)];
    self.label.backgroundColor=[UIColor redColor];
    [self.view addSubview:self.label];
    [self.label release];

    self.label.text=self.str;

    NSLog(@"%@",self.arr[0]);

}

-(void)click:(UIButton *)button
{
    ThirdViewController *thirdVC=[[ThirdViewController alloc] init];
    [self.navigationController pushViewController:thirdVC animated:YES];
    [thirdVC 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
//
//  ThirdViewController.m
//  UI07_导航视图控制器
//
//  Created by dllo on 15/8/6.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "ThirdViewController.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor yellowColor];
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(200, 100, 100, 40);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.layer.borderWidth=1;
    button.layer.cornerRadius=10;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(click:) forControlEvents:
     UIControlEventTouchUpInside];



}
-(void)click:(UIButton *)button
{
    //从后往前跳
    [self.navigationController popToRootViewControllerAnimated:YES ];
    //跳到上一页
//    [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
<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

iOS UI07_导航视图控制器