美文网首页
代理传值

代理传值

作者: 浅笑嫣然9611 | 来源:发表于2017-06-03 15:35 被阅读0次

AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//创建window

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

//根视图控制器

self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[FirstViewController new]];

//显示控制器

[self.window makeKeyWindow];

return YES;

}


FirstViewController.m

#import "FirstViewController.h"

#import "SecondViewController.h"

//遵循SecondVCDelegate代理

@interface FirstViewController ()@property(nonatomic,strong)UITextField *textField;

@end

@implementation FirstViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor cyanColor];

[self creatUI];

}

-(void)creatUI{

//方法一

//self.textField = [UITextField new];

//方法二

self.textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];

self.textField.backgroundColor = [UIColor whiteColor];

[self.view addSubview:self.textField];

UIButton *pushButton = [UIButton buttonWithType:(UIButtonTypeSystem)];

pushButton.frame = CGRectMake(0, 150, self.view.frame.size.width, 30);

[pushButton setTitle:@"push" forState:(UIControlStateNormal)];

[pushButton setTitleColor:[UIColor yellowColor] forState:(UIControlStateNormal)];

//点击事件

[pushButton addTarget:self action:@selector(PushClick:) forControlEvents:UIControlEventTouchUpInside];

//添加button

[self.view addSubview:pushButton];

}

//按钮点击事件

-(void)PushClick:(UIButton *)sender{

SecondViewController *secondVC = [SecondViewController new];

//给二级页面设置代理人(一级页面)

secondVC.delegate = self;

//进入SecondViewController

[self.navigationController pushViewController:secondVC animated:YES];

}

//代理人实现协议方法

-(void)changValue:(NSString *)name{

self.textField.text = name;

}

- (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.

}

*/

@end

#import#import "SecondViewController.h"/* 代理传值 -- > 反向传值(二传一) 从后向前传值 二级页面定义协议和声明代理,一级页面确认并实现代理,一级页面就为二级页面的代理 *///第一步:在二级页面中//1.定义协议//2.设置协议中的方法//3.声明代理//4.在实现中为其绑定方法#warning 声明协议@protocol SecondVCDelegate-(void)changValue:(NSString *)name;@end@interface SecondViewController : UIViewController//声明属性@property(nonatomic,assign)iddelegate;

@end


SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic,strong)UITextField *textField2;

//let KscreenWidth = UIScreen.main.bound.size.width

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor purpleColor];

//    self.view.backgroundColor = [UIColor yellowColor];

//    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, 30)];

//

//

//    lable.backgroundColor = [UIColor whiteColor];

//    [self.view addSubview:lable];

[self creatUI];

}

-(void)creatUI{

//方法一

//self.textField = [UITextField new];

//方法二

self.textField2 = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];

self.textField2.backgroundColor = [UIColor whiteColor];

[self.view addSubview:self.textField2];

UIButton *pushButton = [UIButton buttonWithType:(UIButtonTypeSystem)];

pushButton.frame = CGRectMake(0, 150, self.view.frame.size.width, 30);

[pushButton setTitle:@"pop" forState:(UIControlStateNormal)];

[pushButton setTitleColor:[UIColor yellowColor] forState:(UIControlStateNormal)];

//点击事件

[pushButton addTarget:self action:@selector(PopClick:) forControlEvents:UIControlEventTouchUpInside];

//添加button

[self.view addSubview:pushButton];

}

//实现button方法

-(void)PopClick:(UIButton *)button{

//让代理人执行协议方法

[self.delegate changValue:self.textField2.text];

//返回上一页面

[self.navigationController popViewControllerAnimated:YES];

}

- (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.

}

*/

@end

#import "SecondViewController.h"

/* 代理传值 -- > 反向传值(二传一) 从后向前传值 二级页面定义协议和声明代理,一级页面确认并实现代理,一级页面就为二级页面的代理 *///第一步:在二级页面中//1.定义协议//2.设置协议中的方法//3.声明代理//4.在实现中为其绑定方法

#warning 声明协议

@protocol SecondVCDelegate-(void)changValue:(NSString *)name;

@end

@interface SecondViewController : UIViewController//声明属性@property(nonatomic,assign)id<SecondVCDelegete>delegate;

@end

相关文章

  • Swift常用的界面传值(属性传值、协议传值、闭包传值)

    1、属性传值 属性传值多用于正向传值(A->B) 2、代理传值 代理传值多用于反向传值(B->A) 3、闭包传值 ...

  • block开发使用场景

    block传值 利用block去代理代理传值 1. 代理传值 点击当前控制器Viewcontroller利用mod...

  • iOS简单block的传值

    一直对block传值理解不深,觉得很复杂,所有遇见传值的问题大多数还是用代理传值,但是代理传值还得注册协议实现代理...

  • iOS中界面传值的几种方式

    1.属性传值 属性传值适用于顺序传值,从前面的界面传值给后面的界面。 2.代理传值 用代理的方式实现界面间传值稍微...

  • iOS-传值方式

    传值方式:1、属性传值 方法传值2、代理传值3、单例传值 4、通知传值 NSNotificationCente...

  • 代理

    一:代理传值 A跳转到B,若传值,直接属性即可,但是B返回到A若传值该怎么传值呢? 代理: A的声明中 建立协议 ...

  • Swift界面传值

    Swift中界面传值的方法 主要有三种 1.代理传值2.闭包传值(即OC中的Block) 属性传值 代理传值 F...

  • iOS 传值方式

    1,从前向后传值:属性传值 2, 从后向前传值: block、 代理、 通知 结论1, block、 代理 传值用...

  • iOS 传值的方式的区别

    1.属性传值。 2.block传值。 3.代理传值(delegate) 4.通知传值(notification)。...

  • iOS的五种传值

    前言 iOS常见的五种传值分别为属性传值,通知传值,代理传值,block传值,单例传值 属性传值 用于正向传值,简...

网友评论

      本文标题:代理传值

      本文链接:https://www.haomeiwen.com/subject/pqqmfxtx.html