从前页往后页传递
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];(传递数据)
}
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;
}