ios实现页面间传值

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);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值