UI基础 属性代理传值
rootview.h
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface RootViewController : UIViewController @end
rootview.m
#import "RootViewController.h" #import "SecondViewController.h" @interface RootViewController ()<ChuanZhiDelegate> { UITextField *tf; } @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; // // view.backgroundColor=[UIColor redColor]; // [self.view addSubview:view]; // //把控制导航栏内容代码写到对应的页面上来 self.navigationItem.title=@"首页"; // self.navigationItem.titleView= //左右侧的按钮 文字 // self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"右侧" style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)]; // 图片 self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Processsettings"] style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)]; // 第一个页面中的输入框 页面填充框 tf =[[UITextField alloc]initWithFrame:CGRectMake(20, 200, 280, 40)]; tf.backgroundColor=[UIColor greenColor]; [self.view addSubview:tf]; } -(void)Chaunzhi:(NSString *)str { NSLog(@"验证有没有传值成功%@",str); } -(void)touchRight { NSLog(@"右侧"); SecondViewController *second =[[SecondViewController alloc]init]; //传值 second.inpurStr=tf.text; second.delegate=self; //跳转 [self.navigationController pushViewController:second animated:YES]; } @end
secondview.h
#import <UIKit/UIKit.h> //1.协议的名称 声明协议 @protocol ChuanZhiDelegate <NSObject> -(void)Chaunzhi:(NSString *)str; @end NS_ASSUME_NONNULL_BEGIN @interface SecondViewController : UIViewController //声明一个属性用来接收传递过来的值 @property(nonatomic,strong)NSString *inpurStr; //第二步 声明属性 (代理人) @property(nonatomic,assign)id<ChuanZhiDelegate>delegate; @end
secondview.m
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor =[UIColor greenColor]; // 第二个页面左上角返回按钮 self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)]; //第二个页面中的label UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(20, 200, 280, 40)]; label.backgroundColor=[UIColor greenColor]; label.text=self.inpurStr; [self.view addSubview:label]; } -(void)goBack { //返回的时候触发方法 将值传递回去 [self.delegate Chaunzhi:@"这个是要返回的值"]; // 从哪里回到上一个 [self.navigationController popToRootViewControllerAnimated:YES]; } @end