码迷,mamicode.com
首页 > 移动开发 > 详细

iOS中多视图的传值 属性传值和代理传值

时间:2016-03-15 23:38:33      阅读:588      评论:0      收藏:0      [点我收藏+]

标签:

首先创建两个类 ,FirstViewController和SecondViewController,都继承于UIViewController

 1 #import "AppDelegate.h"
 2 #import "FirstViewController.h"
 3 
 4 @interface AppDelegate ()
 5 
 6 @end
 7 
 8 @implementation AppDelegate
 9 
10 
11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
12     //初始化视图控制器
13     FirstViewController *firstVc=[[FirstViewController alloc]init];
14     //设置根视图控制器
15     self.window.rootViewController=firstVc;
16     return YES;
17 }
1 #import <UIKit/UIKit.h>
2 #import "SecondViewController.h"
3 @interface FirstViewController : UIViewController<UITextFieldDelegate,posVlueDelegate>
4 @property(strong,nonatomic) UITextField *textName;
5 @end
 1 #import "FirstViewController.h"
 2 
 3 @interface FirstViewController ()
 4 
 5 @end
 6 
 7 @implementation FirstViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     //设置背景视图的颜色
12     self.view.backgroundColor=[UIColor redColor];
13     //初始化
14     self.textName=[[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 40)];
15     //设置文本框的颜色
16     self.textName.backgroundColor=[UIColor whiteColor];
17     self.textName.borderStyle=1;
18     [self.view addSubview:self.textName];
19     
20 }
21 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
22 {
23     SecondViewController *secondVc=[[SecondViewController alloc] init];
24     //设置属性值 即 传值给SecondViewController
25     secondVc.str=self.textName.text;
26     //进入第二页
27     [self presentViewController:secondVc animated:YES completion:^{
28         NSLog(@"FirstViewController切换到SecondViewController");
29     }];
30     //指定代理
31     secondVc.delegate=self;
32 }
33 //实现协议方法
34 -(void)posVlue:(NSString *)str
35 {
36     self.textName.text=str;
37 }
38 
39 
40 
41 
42 
43 
44 
45 - (void)didReceiveMemoryWarning {
46     [super didReceiveMemoryWarning];
47     // Dispose of any resources that can be recreated.
48 }
 1 #import <UIKit/UIKit.h>
 2 //创建协议
 3 @protocol posVlueDelegate<NSObject>
 4 -(void)posVlue:(NSString *)str;//协议方法
 5 
 6 @end
 7 
 8 @interface SecondViewController : UIViewController
 9 @property(strong,nonatomic)UITextField *textName;
10 @property(strong,nonatomic)NSString *str;
11 //创建协议类型的属性
12 @property(assign,nonatomic)id<posVlueDelegate>delegate;
13 @end
 1 #import "SecondViewController.h"
 2 
 3 @interface SecondViewController ()
 4 
 5 @end
 6 
 7 @implementation SecondViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     self.view.backgroundColor=[UIColor grayColor];
12     self.textName=[[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 50)];
13     self.textName.backgroundColor=[UIColor whiteColor];
14     self.textName.borderStyle=1;
15     [self.view addSubview:self.textName];
16     //接受FirstViewController传过来的值
17     self.textName.text=self.str;
18 }
19 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
20 {
21     [self dismissViewControllerAnimated:YES completion:^{
22         NSLog(@"SecondViewController切换到FirstViewController");
23     }];
24     
25     if (self.delegate) {
26         [self.delegate posVlue:self.textName.text];
27     }
28 }
29 
30 
31 - (void)didReceiveMemoryWarning {
32     [super didReceiveMemoryWarning];
33     // Dispose of any resources that can be recreated.
34 }

总结:

1.页面之间传值方式

       属性传值

       适用于 正向传值

     1.1 在要显示信息的页面创建属性

     1.2 在要传值的页面 设置属性值

     1.3 在显示页面的viewDidLoad方法中,接受属性值

 

代理传值

 适用于反向传值

 1.创建协议及协议方法,在反向传值的页面SecondViewController中

 2.创建协议类型的属性,在SecondViewController中创建属性

    @property(assign,nonatomic) id<posVlueDelegate> delegate;

 3.调用属性即 delegate

    SecondViewController页面中 对象传值的方法中调用

 if (self.delegate) {

 [self.delegate posVlue:self.textName.text];

 }

 4.在第一页 即显示修改富哦信息的页面

 遵循协议 实现协议方法 指定代理对象(即 在页面传递参数的方法中为代理赋值-(void)posVlue:(NSString *)str

 {

 self.textField.text=str;

 })

 

iOS中多视图的传值 属性传值和代理传值

标签:

原文地址:http://www.cnblogs.com/zhaochaobin/p/5281629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!