UINavigationControlle(导航视图控制器)控制下的页面跳转与值的传递(协议传值)

从前页往后页传递

SecondViewController.h

@interface SecondViewController : UIViewController

(1).定义属性.属性传值

@property(nonatomic, assign)NSInteger number;


MainViewController.m

因为传递是在点击按钮时候发生的,所以要把传递的过程写在buttuon的点击方法里

-(void)click:(UIButton *)button

{

跳转到 SecondViewController

 SecondViewController *secVC = [[SecondViewController alloc]init];

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

 [secVC release];

跳转时,进行属性传值

secVC.number = 100;

}

SecondViewController.m

将传的值打印出来

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

----------------------------------------------------------------------------------------------------------------- 传递字符串和数组

过程:将MainViewController.m中UITextField中输入的数据或者字符串与数组当中保存的数据传递到SecondViewController.m里,


MainViewController.m

定义属性

@property(nonatomic, retain)UITextField *myextField;


创建myextField

[self.myextField release]


传递是在点击时发生的,所以在点击方法里要写

-(void)click:(UIButton *)button

{

跳转

SecondViewController *secVC = [[SecondViewController alloc]init];

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

[secVC release];

传递被输入的数据

secVC.str = self.myextField.text;

secVC.arr = @[@"杨丽坤", @"吴某"];

}



SecondViewController.h

定义字符串和数组的属性用于接收传递过来的字符串和数组

@property(nonatomic, copy)NSString *str;

@property(nonatomic, retain)NSArray *arr;


SecondViewController.m

定义laber属性用于显示从myextField传递过来的被输入的数据

@property(nonatomic, retain)UILabel *laber;

创建laber

[self.laber release];

把属性里的内容对laber赋值(显示)

self.laber.text = self.str;

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

---------------------------------------------------------------------------------------------------------

从后往前传递(需要协议传值)


准备工作

MainViewController.m

@property(nonatomic, retain)UILabel *laber;(用于显示从后面传来的数据)

@property(nonatomic, retain)UIButton *button;(实现跳转并在跳转时设置代理人)


SecondViewController.m

@property(nonatomic, retain)UITextField *textField;(用于输入文本)

@property(nonatomic, retain)UIButton *button;(实现跳转和传递)


分别初始化这四条属性


SecondViewController.h


声明一份协议

@protocol SecondViewControllerDelegate <NSObject>

协议方法

- (void)changeValue:(NSString *)value;

@end


@interface SecondViewController : UIViewController

设置代理人

@property(nonatomic, assign)id<SecondViewControllerDelegate>delegate;


SecondViewController.m


button的点击方法

-(void)click:(UIButton *)button

{

    [self.navigationController popViewControllerAnimated:YES];(跳转)

    [self.delegate changeValue:self.textField.text];(传递数据)

}


MainViewController.m

button的点击方法

-(void)click:(UIButton *)button

{

    push 下一页

    SecondViewController *secVC = [[SecondViewController alloc]init];

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

    [secVC release];


    设置代理人

    secVC.delegate = self;

}


实现协议方法(传值)

-(void)changeValue:(NSString *)value

{

    NSLog(@"%@", value);

    self.laber.text = value;

}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值