你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UI07_導航視圖控制器

iOS UI07_導航視圖控制器

編輯:IOS開發綜合
//
//  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 
@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

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved