iOS 各种传值方式

属性传值 

将A页面所拥有的信息通过属性传递到B页面使用

B页面定义了一个naviTitle属性,在A页面中直接通过属性赋值将A页面中的值传到B页面。

A页面DetailViewController.h文件 

1 #import <UIKit/UIKit.h>
2 #import "DetailViewController.h"
3 @interface RootViewController :UIViewController<ChangeDelegate>
4 {
5     UITextField *tf;
6 }
7 @end

A RootViewController.m页面实现文件

 1 #import "RootViewController.h"
 2 #import "DetailViewController.h"
 3 
 4 @interface RootViewController ()
 5 @end
 6 
 7 @implementation RootViewController
 8 //核心代码
 9 -(void)loadView
10 {
11     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
12     btn.frame = CGRectMake(0, 0, 100, 30);
13     [btn setTitle:@"Push" forState:0];
14     [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
15     [self.view addSubview:btn];
16 }
17 
18 -(void)pushAction:(id)sender
19 {
20     tf = (UITextField *)[self.viewviewWithTag:1000];
21     //导航push到下一个页面
22     //pushViewController 入栈引用计数+1,且控制权归系统
23     DetailViewController *detailViewController = [[DetailViewControlleralloc]init];
24     //属性传值,直接属性赋值
25     detailViewController.naviTitle =tf.text;
26     //导航push到下一个页面
27     [self.navigationControllerpushViewController:detailViewController animated:YES];
28     [detailViewControllerrelease];   
29 }

B页面DetailViewController.h文件

1 #import <UIKit/UIKit.h>
2 @interface DetailViewController :UIViewController
3 {
4    UITextField *textField;
5    NSString *_naviTitle;
6 }
7 @property(nonatomic,retain)NSString *naviTitle;
8 @end

B页面.m实现文件

 1 #import "DetailViewController.h"
 2 @interface DetailViewController ()
 3 @end
 4 @implementation DetailViewController
 5 @synthesize naviTitle =_naviTitle;
 6 -(void)loadView
 7 {
 8     self.view = [[[UIViewalloc]initWithFrame:CGRectMake(0,0, 320,480)]autorelease];
 9    self.title = self.naviTitle ;    
10 }

代理传值 

A页面push到B页面,如果B页面的信息想回传(回调)到A页面,用用代理传值,其中B定义协议和声明代理,A确认并实现代理,A作为B的代理

A页面RootViewController.h文件

1 #import <UIKit/UIKit.h>
2 #import "DetailViewController.h"
3 @interface RootViewController : UIViewController<ChangeDelegate>
4 {
5     UITextField *tf;
6 }
7 @end

A页面RootViewController.m实现文件

 1 #import "RootViewController.h"
 2 #import "DetailViewController.h"
 3 
 4 @interface RootViewController ()
 5 @end
 6 
 7 @implementation RootViewController
 8 //核心代码
 9 -(void)loadView
10 {
11     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
12     btn.frame = CGRectMake(0, 0, 100, 30);
13     [btn setTitle:@"Push" forState:0];
14     //A页面push到B页面
15     [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
16     [self.view addSubview:btn];
17 }
18 
19 -(void)pushAction:(id)sender
20 {
21     tf = (UITextField *)[self.view viewWithTag:1000];
22 
23     //导航push到下一个页面
24     //pushViewController 入栈引用计数+1,且控制权归系统
25     DetailViewController *detailViewController = [[DetailViewController alloc]init]; 
26  
27      //代理传值
28    detailViewController.delegate =self;//让其自身作为代理人
29 
30     //导航push到下一个页面
31     [self.navigationController pushViewController:detailViewController animated:YES];
32     [detailViewController release];   
33 }
34 
35 //实现代理方法
36 -(void)changeTitle:(NSString *)aStr
37 {
38     tf = (UITextField *)[self.view viewWithTag:1000];
39     tf.text = aStr;//将从B页面传入的参数赋给A页面中的TextField
40     tf.text = aStr;
41 }

B页面DetailViewController.m文件

 1 #import <UIKit/UIKit.h>
 2 @interface DetailViewController : UIViewController
 3 {
 4     UITextField *textField;
 5     //定义代理
 6     id<ChangeDelegate>_delegate;
 7 }
 8  
 9 @property(nonatomic,assign)id<ChangeDelegate> delegate;
10 @end
11 
12 //定义协议
13 @protocol ChangeDelegate <NSObject>
14 -(void)changeTitle:(NSString *)aStr;//协议方法
15 @end

B页面DetailViewController.h实现文件

 1 #import "DetailViewController.h"
 2 @interface DetailViewController ()
 3 @end
 4 
 5 @implementation DetailViewController
 6 -(void)loadView
 7 {
 8     self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
 9     UIBarButtonItem *doneItem = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(doneAction:)];
10     self.navigationItem.rightBarButtonItem = doneItem;
11     [doneItemrelease];
12 }
13 
14 //pop回前一个页面
15 -(void)doneAction:(id)sender
16 {
17    if (self.delegate && [self.delegaterespondsToSelector:@selector(changeTitle:)])//若代理存在且响应了changeTitle这个方法
18     {
19         //[self.delegate changeTitle:textField.text];
20         [self.delegatechangeTitle:textField.text];//将textField.text参数传给changeTitle方法  让代理,也就是A页面去实现这个方法
21 
22         NSLog(@"%@",self.navigationController.viewControllers);
23         [self.navigationControllerpopViewControllerAnimated:YES];
24     }
25

单例传值(实现共享)

AppStatus.h  创建一个单例类 AppStatus

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface AppStatus : NSObject
 4 {
 5     NSString *_contextStr;
 6 }
 7 
 8 @property(nonatomic,retain)NSString *contextStr;
 9 
10 +(AppStatus *)shareInstance;
11 
12 @end

AppStatus.m

 1 #import "AppStatus.h"
 2 
 3 @implementation AppStatus
 4 
 5 @synthesize contextStr = _contextStr;
 6 
 7 static AppStatus *_instance = nil;
 8 
 9 +(AppStatus *)shareInstance
10 {
11     if (_instance == nil)
12     {
13         _instance = [[super alloc]init];
14     }
15     return _instance;
16 }
17 
18 -(id)init
19 {
20     if (self = [super init])
21     {
22         
23     }
24     return self;
25 }
26 
27 -(void)dealloc
28 {
29     [super dealloc];
30 }
31 
32 @end

A页面RootViewController.h

 1 #import "RootViewController.h"
 2 #import "DetailViewController.h"
 3 #import "AppStatus.h"
 4 
 5 @interface RootViewController ()
 6 
 7 @end
 8 
 9 @implementation RootViewController
10 
11 -(void)loadView
12 {
13     //核心代码 
14     UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
15     btn.frame = CGRectMake(0, 0, 100, 30);
16     [btn setTitle:@"Push" forState:0];
17     [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
18     [self.view addSubview:btn];
19 }
20 
21 -(void)pushAction:(id)sender
22 {
23     tf = (UITextField *)[self.view viewWithTag:1000];
24 
25  //单例传值  将要传递的信息存入单例中(共享中)
26   //  [[AppStatus shareInstance]setContextStr:tf.text]; 跟下面这种写法是等价的
27     [AppStatus shareInstance].contextStr = tf.text;
28     //导航push到下一个页面
29     //pushViewController 入栈引用计数+1,且控制权归系统
30     DetailViewController *detailViewController = [[DetailViewController alloc]init];
31 
32     //导航push到下一个页面
33     [self.navigationController pushViewController:detailViewController animated:YES];
34     [detailViewController release];
35 } 
36 
37 @end

B页面DetailViewController.h

1 #import <UIKit/UIKit.h>
2 @protocol ChangeDelegate;//通知编译器有此代理
3 
4 @interface DetailViewController : UIViewController
5 {
6     UITextField *textField;
7 }
8 
9 @end

B页面DetailViewController.m

 1 #import "DetailViewController.h"
 2 #import "AppStatus.h"
 3 
 4 @interface DetailViewController ()
 5 
 6 @end
 7 
 8 @implementation DetailViewController
 9 
10 @synthesize naviTitle = _naviTitle;
11 
12 -(void)loadView
13 {
14     self.view = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]autorelease];
15 
16     //单例
17     self.title = [AppStatus shareInstance].contextStr;
18     textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
19     textField.borderStyle = UITextBorderStyleLine;
20     [self.view addSubview:textField];
21     [textField release];
22 
23     UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];
24     self.navigationItem.rightBarButtonItem = doneItem;
25     [doneItem release];
26 }
27 
28 //这个方法是执行多遍的  相当于刷新view
29 -(void)viewWillAppear:(BOOL)animated
30 {
31     [super viewWillAppear:animated];
32     tf = (UITextField *)[self.view viewWithTag:1000];
33     tf.text = [AppStatus shareInstance].contextStr;
34 }
35 
36 //pop回前一个页面
37 -(void)doneAction:(id)sender
38 {
39     //单例传值
40     [AppStatus shareInstance].contextStr = textField.text;
41     [self.navigationController popToRootViewControllerAnimated:YES];
42 } 

通知传值

谁要监听值的变化,谁就注册通知  特别要注意,通知的接受者必须存在这一先决条件

A页面RootViewController.h

1 #import <UIKit/UIKit.h>
2 #import "DetailViewController.h"
3 @interface RootViewController : UIViewController<ChangeDelegate>
4 {
5     UITextField *tf;
6 }
7 @end 

A页面RootViewController.m

 1 #import "IndexViewController.h"
 2 #import "DetailViewController.h"
 3 #import "AppStatus.h"
 4 
 5 @implementation IndexViewController
 6 
 7 -(void)dealloc
 8 {
 9     [[NSNotificationCenter defaultCenter] removeObserver:self
10                                                     name:@"CHANGE_TITLE" object:nil];
11     [super dealloc];
12 }
13 
14 -(id)init
15 {
16     if (self = [super init])
17     {
18         [[NSNotificationCenter defaultCenter] addObserver:self
19                                                  selector:@selector(change:)
20                                                      name:@"CHANGE_TITLE"
21                                                    object:nil];
22     }
23     return self;
24 }
25 
26 -(void)change:(NSNotification *)aNoti
27 {
28     // 通知传值
29     NSDictionary *dic = [aNoti userInfo];
30     NSString *str = [dic valueForKey:@"Info"];
31     
32     UITextField *tf =  (UITextField *)[self.view viewWithTag:1000];
33     tf.text = str;
34 }
35  
36 -(void)viewWillAppear:(BOOL)animated
37 {
38     [super viewWillAppear:animated];
39     /*
40     // 单例传值
41     UITextField *tf =  (UITextField *)[self.view viewWithTag:1000];
42     tf.text = [AppStatus shareInstance].contextStr;
43     */
44 }
45 
46 @end

DetailViewController.h

1 #import <UIKit/UIKit.h>
2 @protocol ChangeDelegate;//通知编译器有此代理
3 
4 @interface DetailViewController : UIViewController
5 {
6     UITextField *textField;
7 }
8 @end

 DetailViewController.m

 1 #import "DetailViewController.h"
 2 #import "AppStatus.h"
 3 
 4 @implementation DetailViewController
 5 @synthesize naviTitle = _naviTitle;
 6 
 7 -(void)loadView
 8 {
 9     UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];
10     self.navigationItem.rightBarButtonItem = doneItem;
11     [doneItem release];
12 }
13 
14 // pop回前一个页面
15 -(void)doneAction:(id)sender
16 {
17 NSDictionary *dic = [NSDictionary dictionaryWithObject:textField.text forKey:@"Info"];
18 
19 [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_TITLE" object:nil userInfo:dic];
20 
21 [self.navigationController popViewControllerAnimated:YES];
22 
23 }

Block

几种形式的Block

 1 //无返回值
 2     void (^block1) (void);
 3     block1 = ^{
 4         NSLog(@"bock demo");
 5     };
 6     block1();
 7     
 8     //int返回类型
 9     int (^block2) (void);
10     block2  = ^(void)
11     {
12         int a  = 1 ,b =1;
13         int c = a+b;
14         return  c;
15     };
16     
17     //有返回 有参数
18     int (^block3)(int, int)= ^(int a, int b)
19     {
20         int c = a +b;
21         return c;
22         
23     };
24     NSLog(@"bock=%d",block3(1,2));
25     
26     //有返回值,有参数并且可以修改block之外变量的block
27     static int sum = 10;// __blcik and static关键字 或者 _block int sum = 10
28     int (^block4) (int) =^(int a)
29     {
30         sum=11;
31         int c = sum+a;   //此时sum就是可以修改的了,若没加static或_block关键字则不能修改block之外变量
32         return c;
33     };
34     NSLog(@"block4= %d",block4(4));

Block传值

例如A(Ablock)页面的值传道B(Bblock)页面  

在A页面中ABlock.h

 1 @interface Ablock : UIViewController<UITableViewDelegate,UITableViewDataSource>
 2 {
 3     UITableView *_tableview;
 4     UILabel *labe;
 5     UIImageView *imagevies;
 6 }
 7 @end
 8 
 9 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
10 {
11     [_tableview deselectRowAtIndexPath:indexPath animated:YES];
12     
13     Bblcok *bblock = [[Bblcok alloc] initwithBlock:Block_copy(^(NSString *aBlock){    
14         labe.text = aBlock;
15         NSLog(@"%@",aBlock);
16 
17     })];
18     
19     bblock.imgeviews = imagevies.image;
20     bblock.String = labe.text;
21     [self.navigationController pushViewController:bblock animated:YES];
22     [bblock release];
23 }

在A页面中Bblock.h

 1 #import <UIKit/UIKit.h>
 2 typedef  void (^MyBlock) (NSString *);
 3 @interface Bblcok : UIViewController
 4 {
 5     UIImageView *image;
 6     UITextField *aField;
 7     UIButton *aButt;
 8     NSString *_String;
 9     id _imgeviews;
10     MyBlock myBlock;
11 }
12 @property(nonatomic,copy)MyBlock myBlock;   
13 @property(nonatomic,retain) id imgeviews;
14 @property(nonatomic,retain) NSString *String;
15 -(id)initwithBlock:(MyBlock)aBlcok;
16 @end

Bblcok.m

 1 #import "Bblcok.h"
 2 
 3 @interface Bblcok ()
 4 @end
 5 
 6 @implementation Bblcok
 7 @synthesize imgeviews = _imgeviews , String = _String;
 8 @synthesize myBlock = _myBlock;
 9 -(id)initwithBlock:(MyBlock)aBlcok
10 {
11     if (self = [super init])
12     {
13         self.myBlock = aBlcok; 
14     }
15     return self;
16 }
17 
18 -(void) dealloc
19 {
20     [super dealloc];
21 }
22 
23 -(void) loadView
24 {
25     UIControl *cont = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320, 568-44)];
26     [cont addTarget:self action:@selector(Clcik) forControlEvents:UIControlEventTouchUpInside];
27     self.view = cont;
28     
29     aField = [[UITextField alloc] initWithFrame:CGRectMake(60, 10, 160, 30)];
30     aField.borderStyle = UITextBorderStyleLine;
31     aField.placeholder = self.String;
32     [self.view addSubview:aField];
33     
34     aButt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
35     aButt.frame = CGRectMake(60, 50, 70, 30);
36     [aButt setTitle:@"修改" forState:0];
37     [aButt addTarget:self action:@selector(aButtClcik:) forControlEvents:UIControlEventTouchUpInside];
38     [self.view addSubview:aButt];
39     
40     image = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 210, 260)];
41     image.backgroundColor = [UIColor blueColor];
42     image.image = self.imgeviews;
43     [self.view addSubview:image];
44     [image release];
45 
46 }
47 
48 -(IBAction)aButtClcik:(id)sender
49 {
50     NSString *sting = aField.text;
51     myBlock(sting);
52     [self.navigationController popToRootViewControllerAnimated:YES];
53 }
54 
55 
56 -(void)Clcik
57 {
58     [aField resignFirstResponder];
59 }
60 - (void)viewDidLoad
61 {
62     [super viewDidLoad];
63     // Do any additional setup after loading the view.
64 }
65 
66 - (void)didReceiveMemoryWarning
67 {
68     [super didReceiveMemoryWarning];
69     // Dispose of any resources that can be recreated.
70 }
71 
72 @end

转载于:https://www.cnblogs.com/tryingx/articles/3931961.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值