关闭

IOS--代理传值

标签: IOS代理传值
114人阅读 评论(0) 收藏 举报
分类:

1.逆向传值:

由B页面向A页面传值:

B页面

#import <UIKit/UIKit.h>


@protocol PassTrendValueDelegate

-(void)passTrendValues:(NSString *)values;//定义协议与方法

@end



@interface SecondViewController : UIViewController

///定义向趋势页面传值的委托变量

@property (retain,nonatomic) id <PassTrendValueDelegate> trendDelegate;



@property (nonatomic ,strong) NSString * data;


@end


#import "SecondViewController.h"

#import "FirstViewController.h"

@interface SecondViewController ()


@end


@implementation SecondViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    NSLog(@"data=%@",self.data);

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

- (IBAction)backonclick:(id)sender {

    FirstViewController * first=[[FirstViewController alloc] init];

    self.trendDelegate=first;

    [self.trendDelegate passTrendValues:@"huguobin"];

    [self dismissViewControllerAnimated:YES completion:^{}];

}


@end


A页面

#import <UIKit/UIKit.h>

#import "SecondViewController.h"

@interface FirstViewController : UIViewController<PassTrendValueDelegate>{


    

}


@end


#import "FirstViewController.h"

#import "SecondViewController.h"

@interface FirstViewController ()


@end


@implementation FirstViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

    

    SecondViewController *second=segue.destinationViewController;

    second.data=@"我是第一个页面的值";


}

#pragma mark 实现传值协议方法

-(void)passTrendValues:(NSString *)values{

    NSLog(@"value=%@",values);


}


@end



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:66074次
    • 积分:1863
    • 等级:
    • 排名:千里之外
    • 原创:92篇
    • 转载:158篇
    • 译文:8篇
    • 评论:2条
    最新评论