导航视图控制器及其属性传值

来源:互联网 发布:套利程序化交易软件 编辑:程序博客网 时间:2024/05/11 05:31

导航视图控制器及其属性传值

import “MainViewController.h”

import “SecondViewController.h”

@interface MainViewController ()

@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 yellowColor];

    // 导航视图控制器的高度是44,上面的状态栏高度是20,加在一起默认是64
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(270, 530, 100, 40);
    [self.view addSubview:button];
    [button setTitle:@”下一页” forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    // 对导航视图控制器进行设置
    // 加上一个标题
    // self.title=@”猫眼电影”;

    // 对外观进行设置
    // 背景颜色的设置
    // 不是所有的背景颜色都是backgoundColor
    self.navigationController.navigationBar.barTintColor=[UIColor greenColor];

    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:@[@”游戏”,@”客服”]] autorelease];
    self.navigationItem.titleView=seg;

    // 创建左右两边的按钮
    self.navigationItem.leftBarButtonItem=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftButtonAction:)] autorelease];

    //
    // 这种方式创建就是蓝色
    self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@”1.png”] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)] autorelease];
    UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame=CGRectMake(0, 0, 40, 40);
    [button1 setImage:[UIImage imageNamed:@”1.png”] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:button1];

    self.textField=[[UITextField alloc] initWithFrame:CGRectMake(100, 175, 175, 50)];
    self.textField.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.textField];
    [self.textField release];
    self.textField.layer.borderWidth=1;
    self.textField.layer.cornerRadius=10;
    self.textField.placeholder=@”请输入你想输入的玩意”;

}

-(void)leftButtonAction:(UIBarButtonItem *)barButtonItem{

}

-(void)rightButtonAction:(UIBarButtonItem *)barButtonItem{

}
-(void)click:(UIButton *)button{
// 用模态跳转下一页
// SecondViewController *secondVC=[[SecondViewController alloc] init];
// [secondVC setModalTransitionStyle:UIModalTransitionStylePartialCurl];
// [self presentViewController:secondVC animated:YES completion:^{}];
// [secondVC release];

// 通过导航视图控制器进行跳转SecondViewController *secondVC=[[SecondViewController alloc] init];[self.navigationController pushViewController:secondVC animated:YES];[secondVC release];// 属性传值的第二步secondVC.number=100;secondVC.str=self.textField.text;secondVC.arr=@[@"张三",@"李四",@"王二麻子"];

}

import “SecondViewController.h”

import “ThirdViewController.h”

@interface SecondViewController ()
@property(nonatomic,retain)UILabel *label;

@end

@implementation SecondViewController

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

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor redColor];

    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(270, 530, 100, 40);
    [self.view addSubview:button];
    [button setTitle:@”下一页” forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

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

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

    // 把属性里的内容对label进行赋值
    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];
}

import “ThirdViewController.h”

@interface ThirdViewController ()

@end

@implementation ThirdViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor greenColor];

    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(270, 530, 100, 40);
    [self.view addSubview:button];
    [button setTitle:@”返回” forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)click:(UIButton *)button{
// 从后往前跳
[self.navigationController popToRootViewControllerAnimated:YES];
// 跳到上一页
[self.navigationController popViewControllerAnimated:YES];

}

0 0
原创粉丝点击