您的位置:首页 > 其它

导航视图控制器学习笔记

2015-08-06 20:52 591 查看
//  AppDelegate.m

//  UI07_导航视图控制器

//

//  Created by dllo on 15/8/6.

//  Copyright (c) 2015年 cml. All rights reserved.

//

#import "AppDelegate.h"

#import "MainViewController.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]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor
whiteColor];

    [self.window
makeKeyAndVisible];
    [_window
release];

    

    //
先创建一个ViewController

    MainViewController *mainVC =[[MainViewController
alloc]
init];

    //
创建导航视图控制器

    UINavigationController *naVC=[[UINavigationController
alloc]
initWithRootViewController:mainVC];
   
//

    self.window.rootViewController =naVC;

    //
释放
    [mainVC
release];
    [naVC
release];

//  MainViewController.m

//  UI07_导航视图控制器

//

//  Created by dllo on 15/8/6.

//  Copyright (c) 2015年 cml. 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
orangeColor];

    

    //
导航控制器高度是44,上面的状态栏高度是20,加在一起默认是64

    

    UIButton *button =[UIButton
buttonWithType:UIButtonTypeSystem];
    button.frame =CGRectMake(240,
540, 100,
40);

    button.layer.borderWidth=1;

    button.layer.cornerRadius=10;
    [self.view
addSubview:button];

    [button setTitle:@"下一页"
forState:UIControlStateNormal];

    

    [button addTarget:self
action:@selector(click:)
forControlEvents:UIControlEventTouchUpInside];

    

    

    //
对导航视图进行设置

    //
加上一个标题

//    self.title =@"猫眼电影";

    

    //
外观进行设置

    //
背景颜色的设置

    //
不是所有的背景颜色都是backgroundColor

    //
默认是半透明的

//    self.navigationController.navigationBar.barTintColor=[UIColor greenColor];

    self.navigationController.navigationBar.translucent=NO;

    //
创建一个UIView

    UIView *view=[[UIView
alloc] initWithFrame:CGRectMake(0,
0, 100,
100)];

    [view setBackgroundColor:[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:UIBarButtonSystemItemEdit target:self action:@selector(leftButtonClick:)] autorelease];

    

//    self.navigationItem.leftBarButtonItem =[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leftButtonClick:)] autorelease];

    

    self.navigationItem.leftBarButtonItem
=[[UIBarButtonItem
alloc] initWithImage:[UIImage
imageNamed:@"双子"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(leftButtonClick:)];

    
   
//

//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"双子"] style:UIBarButtonItemStylePlain target:self action:@selector(rigthButtonClick:)];

//    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"双子"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonClick)];

    

    

    //
创建一个小button

    UIButton *rightButton=[UIButton
buttonWithType:UIButtonTypeCustom];
    rightButton.frame=CGRectMake(0,
0, 40,
40);

    [rightButton setImage:[UIImage
imageNamed:@"双子"]
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];

    [_textfield
release];

    
}

-(void)leftButtonClick:(UIBarButtonItem *)barBUtton
{

    
}

-(void)rigthButtonClick:(UIBarButtonItem *)Button{

    
}

-(void)click:(UIButton *)button{

    //
用模态跳转下一页

//    SecondViewController *secondView=[[SecondViewController alloc] init];

//    [secondView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

//    [self presentViewController:secondView animated:YES completion:^{

//    }];

//    [secondView release];

    

    //
通过导航控制器进行跳转

    //
先创建下一页的对象

    SecondViewController *secondView =[[SecondViewController
alloc]
init];
   
//

    //
属性传值的第二步
    secondView.number =100;

    
    secondView.str =self.textfield.text;

    
    secondView.arr =@[@"1",@"2",@"3"];

    

    

    [self.navigationController
pushViewController:secondView animated:YES];
    [secondView
release];

    
}

//

//  SecondViewController.h

//  UI07_导航视图控制器

//

//  Created by dllo on 15/8/6.

//  Copyright (c) 2015年 cml. 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年 cml. All rights reserved.

//

#import "SecondViewController.h"

#import "ThirdViewController.h"

@interface
SecondViewController ()

@property(nonatomic ,retain)UILabel *label;

@end

@implementation SecondViewController

-(void)dealloc{
    [_label
release];
    [_str
release];
    [_arr
release];
    [super
dealloc];
}

- (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(240,
540, 100,
40);

    button.layer.borderWidth=1;

    button.layer.cornerRadius=10;
    [self.view
addSubview:button];

    [button setTitle:@"下一页"
forState:UIControlStateNormal];

    

    [button addTarget:self
action:@selector(click:)
forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *button1 =[UIButton
buttonWithType:UIButtonTypeSystem];
    button1.frame =CGRectMake(120,
540, 100,
40);

    button1.layer.borderWidth=1;

    button1.layer.cornerRadius=10;
    [self.view
addSubview:button1];

    [button1 setTitle:@"上一页"
forState:UIControlStateNormal];

    

    [button1 addTarget:self
action:@selector(click1:)
forControlEvents:UIControlEventTouchUpInside];
   
NSLog(@"%ld",self.number);

    

    

    self.label =[[UILabel
alloc] initWithFrame:CGRectMake(100,
100,
100, 40)];

    self.label.backgroundColor =[UIColor
cyanColor];
    [self.view
addSubview:self.label];
    [self.label
release];

    

    //
把属性里的内容对label进行赋值
   
self.label.text =self.str;

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

    

    

    
}
-(void)click:(UIButton *)button{

    ThirdViewController *thirdView=[[ThirdViewController
alloc]
init];

   

    [self.navigationController
pushViewController:thirdView animated:YES];
    [thirdView
release];

    
}

-(void)click1:(UIButton *)button{

    [self.navigationController
popViewControllerAnimated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: