1.简单的传值,使用类、属性
在viewcontrol2中声明一个接受传值的变量(需要在.h中声明)。
使用:在跳转时调用----代码如下:
TwoActivity *t=[[TwoActivity alloc]init];
t.s=@"sb";
self.delegate=t;
[self.navigationController pushViewController:t animated:YES];
2.使用代理传值在viewcontrol1中声明协议,并在属性中持有改协议,在m中实现该协议,在跳转时为协议注明代理为跳转到的viewcontrol,后调用协议中方法,即可实现传值,viewcontrol2也必须实现该协议,代码如下:
viewcontrol1.h:
#import <UIKit/UIKit.h>
@protocol MyDelegate
-(void)sendValue:(NSString*)values;
@end
@interface ViewController : UIViewController
@end
============
#viewcontrol1.m#import "ViewController.h"
#import "TwoActivity.h"
@interface ViewController ()
@property (retain,nonatomic) id <MyDelegate> delegate;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *back= [[UIView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
back.backgroundColor=[UIColor blueColor];
[self.view addSubview:back];
// Do any additional setup after loading the view, typically from a nib.
TwoActivity *t=[[TwoActivity alloc]init];
self.delegate=t;
[self.delegate sendValue:@"test"];
[self.navigationController pushViewController:t animated:YES];
}
====================viewcontrol2.m
#import "TwoActivity.h"
#import "ViewController.h"
@interface TwoActivity ()<MyDelegate>
@end
@implementation TwoActivity
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",self.s);
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)sendValue:(NSString *)values{
//这里方法的参数就是传递过来的数据
NSLog(@"%@",values);
}