iOS中的UINavigationController


UINavigationController的简单应用:


创建两个视图控制器 MainViewController和SecondViewController 实现第一个视图控制器上的textField上的值能能传递到第二个视图控制器上的textField上  反之也可以.



AppDelegate.m

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

    //创建一个导航控制器对象 管理main

    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:main];

    //设置导航栏的背景色

    [nav.navigationBar setBarTintColor:[UIColor orangeColor]];

    //入口

    [_window setRootViewController:nav];

    [main release];

    [nav release];



MainViewController.h

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

//实现协议

@interface MainViewController : UIViewController<SecondViewControllerProtocol>

@property  (nonatomic,retain) UITextField *firstText;

@end


MainViewController.m

#import "MainViewController.h"

#import "SecondViewController.h"

@interface MainViewController ()


@end


@implementation MainViewController

- (void)dealloc

{

    [_firstText release];

    _firstText = nil;

    [super dealloc];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

- (void)loadView

{

    [super loadView];

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    //设置透明度

    self.navigationController.navigationBar.translucent = NO;

    


    //设置titleview

    

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 40, 20)];

    [label setText:@"LoVeLin"];

    [self.navigationItem setTitleView:label];

//    [self.navigationItem setTitle:@"LoVeLin"];

    [label release];

    

    //设置导航栏的右按钮

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(20, 20, 40, 20);

    [button setTitle:@"进入" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

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

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:button];

    //添加导航栏的右按钮

    [self.navigationItem setRightBarButtonItem:barButton];

    [barButton release];

    

    //设置背景色

    [self.view setBackgroundColor:[UIColor cyanColor]];

    

    self.firstText = [[[UITextField alloc]initWithFrame:CGRectMake(50, 50, 220, 30)]autorelease];

    [_firstText setBackgroundColor:[UIColor grayColor]];

    [self.view addSubview:_firstText];



    

    


    

    

    

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark 导航栏右按钮点击事件

- (void)buttonAction : (id)sender

{

    

//属性传值 ===============================================================================

    //创建上层的视图控制器

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


//    second.secondTextValue = _firstText.text ;

//初始化方法传值 ==========================================================================

    SecondViewController *second = [[SecondViewController alloc]initWithTextValue:_firstText.text];

//协议传值 ===============================================================================

    

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

    // 设置代理者

    second.delegate = self;

    //点击按钮进入上一层


    

    [self.navigationController pushViewController:second animated:YES];

    [second release];

}

#pragma mark 实现协议的方法

- (void)sendTextValue:(NSString *)textValue

{

    [_firstText setText:textValue];

}


SecondViewController.h


#import <UIKit/UIKit.h>


@protocol SecondViewControllerProtocol <NSObject>


- (void) sendTextValue : (NSString *)textValue;


@end


@interface SecondViewController : UIViewController


//协议传值

@property (nonatomic,retain) UITextField *secondText;

@property (nonatomic,assign) id<SecondViewControllerProtocol>delegate;//代理者



//属性传值

@property (nonatomic,retain) NSString *secondTextValue;


//方法传值

- (id)initWithTextValue : (NSString *)textValue;

@end


SecondViewController.m


#import "SecondViewController.h"


@interface SecondViewController ()


@end


@implementation SecondViewController

- (void)dealloc

{

    [_secondTextValue release];

    [_secondText release];

    _secondTextValue = nil;

    _secondText = nil;

    [super dealloc];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

#pragma mark 初始化方法传值

- (id)initWithTextValue : (NSString *)textValue

{

    self = [super init];

    if (self) {

        self.secondTextValue = textValue; // 把初始化时传进来的值给secondText赋值

        

    }

    return self;

}

- (void)loadView

{

    [super loadView];

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 40, 20)];

    [label setText:@"LoVeLin"];

    [self.navigationItem setTitleView:label];

    [label release];

    

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(20, 20, 40, 20);

    [button setTitle:@"后退" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

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

    

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:button];

    [self.navigationItem setRightBarButtonItem:barButton];

    [barButton release];

    

    

    [self.view setBackgroundColor:[UIColor cyanColor]];

    

    self.secondText = [[[UITextField alloc]initWithFrame:CGRectMake(50, 50, 220, 30)]autorelease];

    [_secondText setBackgroundColor:[UIColor grayColor]];

    [_secondText setText:_secondTextValue];

    [self.view addSubview:_secondText];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark 按钮的点击事件

- (void)buttonAction : (id)sender

{

    [self.delegate sendTextValue:[_secondText text]]; //老板让下属执行协议方法

    [self.navigationController popViewControllerAnimated:YES];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值