现在的位置: 首页 > 综合 > 正文

iOS 代理传值

2018年09月10日 ⁄ 综合 ⁄ 共 947字 ⁄ 字号 评论关闭

之前一直对于代理传值不是很了解很模糊,今天试着写了个demo,

1、在需要传值的界面声明协议,比如从B界面传值给A,那么就在B界面设置代理,然后在A界面添加代理。

在B界面:

.h文件

@protocol TwoViewDelegate <NSObject>

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

@end

@interface GCTwoViewController :
UIViewController

@property (nonatomic,unsafe_unretained)
id<TwoViewDelegate>delegate;

@end

.m文件发生界面跳转的时候-----》A

- (IBAction)btnc:(id)sender {

    

    [self.delegate 
changeValue:self.textFiled.text];

    

    [self
dismissViewControllerAnimated:YES
completion:nil];

}

在需要改变的界面A

.h文件

#import "GCTwoViewController.h"

@interface
GCViewController ()<TwoViewDelegate>


.m文件

- (IBAction)btncli:(id)sender {

    

    [self
performSegueWithIdentifier:@"inputTwoView"
sender:nil];

}

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

{

    

    

   
if ([segue.identifier
isEqualToString:@"inputTwoView"]) {

        

       
GCTwoViewController *twoView = segue.destinationViewController;

        

        twoView.delegate =
self;

        

    }

}

//在B界面返回的时候会调用代理方法

- (void)changeValue:(NSString *)value

{

   
self.label.text = value;

}

原代码如下:

抱歉!评论已关闭.