iOS UI-界面传值(三种方法)

 1 #import <Foundation/Foundation.h>
 2
 3 @interface DataModel : NSObject
 4
 5 @property (nonatomic, copy) NSString *name;
 6 @property (nonatomic, copy) NSString *address;
 7
 8 @end
 9
10 #import "DataModel.h"
11
12 @implementation DataModel
13
14 @end
  1 #import <UIKit/UIKit.h>
  2 @class DataModel;
  3 //设置协议
  4 @protocol PassValueDelegate <NSObject>
  5 - (void) passValueFromFirstVCToSecondVC:(DataModel *) dataModel;
  6 @end
  7
  8 @interface FirstViewController : UIViewController
  9 //设置代理
 10 @property (nonatomic, strong) id<PassValueDelegate> delegate;
 11 //设置通知传值接受者
 12 @property (nonatomic, strong) DataModel *firstNOtificationModel;
 13 //设置代理传值传值者
 14 @property (nonatomic, strong) DataModel *firstDelegateModel;
 15
 16 @end
 17
 18
 19 #import "FirstViewController.h"
 20 #import "DataModel.h"
 21 #import "SecondViewController.h"
 22
 23
 24 @interface FirstViewController ()
 25 //设置接收通知传值的标签
 26 @property (nonatomic, strong)UILabel *label;
 27 //设置接收block传值的标签
 28 @property (nonatomic, strong)UILabel *lblBlock;
 29
 30 @end
 31
 32 @implementation FirstViewController
 33
 34 - (void)viewDidLoad {
 35     [super viewDidLoad];
 36     [self.view setBackgroundColor:[UIColor orangeColor]];
 37
 38     [self createButtonAndLabel];
 39
 40     //创建观察者
 41     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveValue:) name:@"passValueFromSecondVCToFirstVC" object:nil];
 42 }
 43
 44
 45 #pragma mark - 创建按钮和标签
 46 - (void)createButtonAndLabel
 47 {
 48     //创建标签
 49     self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 355, 60)];
 50     [self.label setBackgroundColor:[UIColor redColor]];
 51     self.label.textAlignment = NSTextAlignmentCenter;
 52     [self.view addSubview:self.label];
 53
 54     //创建标签
 55     self.lblBlock = [[UILabel alloc] initWithFrame:CGRectMake(10, 420, 355, 60)];
 56     [self.lblBlock setBackgroundColor:[UIColor redColor]];
 57     self.lblBlock.textAlignment = NSTextAlignmentCenter;
 58     [self.view addSubview:self.lblBlock];
 59
 60     //创建按钮
 61     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 62     [btn setFrame:CGRectMake(80, 300, 215, 80)];
 63     [btn setBackgroundColor:[UIColor blackColor]];
 64     [btn setTitle:@"goToNextView" forState:UIControlStateNormal];
 65     [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 66     [btn addTarget:self action:@selector(goToNextView) forControlEvents:UIControlEventTouchUpInside];
 67     [self.view addSubview:btn];
 68 }
 69
 70 #pragma mark - 观察者接收值关联方法
 71 - (void)receiveValue:(NSNotification *)notification
 72 {
 73     if ([notification.object isKindOfClass:[DataModel class]]) {
 74         self.firstNOtificationModel = notification.object;
 75         self.label.text = [NSString stringWithFormat:@"name:%@ address:%@",self.firstNOtificationModel.name,self.firstNOtificationModel.address];
 76     }
 77 }
 78 #pragma mark - 代理传值、block接收值
 79 //按钮关联方法
 80 - (void)goToNextView
 81 {
 82     SecondViewController *secondVC = [[SecondViewController alloc] init];
 83     [self presentViewController:secondVC animated:YES completion:nil];
 84
 85     [secondVC setBlock:^(DataModel *dataModel){
 86         self.lblBlock.text = [NSString stringWithFormat:@"name:%@ address:%@",dataModel.name,dataModel.address];
 87     }];
 88
 89     self.delegate = secondVC;
 90     if ([self.delegate respondsToSelector:@selector(passValueFromFirstVCToSecondVC:)]) {
 91         [self.delegate performSelector:@selector(passValueFromFirstVCToSecondVC:) withObject:self.firstDelegateModel];
 92     }
 93 }
 94
 95 #pragma mark - 懒加载
 96 - (DataModel *)firstDelegateModel
 97 {
 98     if (!_firstDelegateModel) {
 99         _firstDelegateModel = [[DataModel alloc] init];
100         _firstDelegateModel.name = @"GuYu";
101         _firstDelegateModel.address = @"AnHui";
102     }
103     return _firstDelegateModel;
104 }
105
106 @end
 1 #import <UIKit/UIKit.h>
 2 #import "FirstViewController.h"
 3 @class DataModel;
 4 //定义block类型
 5 typedef void(^Block)(DataModel *dataModel);
 6
 7 @interface SecondViewController : UIViewController <PassValueDelegate>
 8 //通知传值的传值者
 9 @property (nonatomic, strong) DataModel *secondNotificationModel;
10 //代理传值的接受者
11 @property (nonatomic, strong) DataModel *secondDelegateModel;
12 //block的接受者
13 @property (nonatomic, strong) Block block;
14
15 @end
16
17
18 #import "SecondViewController.h"
19 #import "DataModel.h"
20
21 @interface SecondViewController ()
22
23 @property (strong, nonatomic) UILabel *label;
24
25 @end
26
27 @implementation SecondViewController
28 - (void)viewDidLoad {
29     [super viewDidLoad];
30     [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
31
32     [self createButtonAndLabel];
33 }
34 #pragma mark - 创建标签和按钮
35 - (void)createButtonAndLabel
36 {
37     //创建标签
38     self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, 355, 60)];
39     [self.label setBackgroundColor:[UIColor redColor]];
40     self.label.textAlignment = NSTextAlignmentCenter;
41     [self.view addSubview:self.label];
42
43     //创建按钮
44     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
45     [btn setFrame:CGRectMake(80, 300, 215, 80)];
46     [btn setBackgroundColor:[UIColor blackColor]];
47     [btn setTitle:@"goToBack" forState:UIControlStateNormal];
48     [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
49     [btn addTarget:self action:@selector(goToBack) forControlEvents:UIControlEventTouchUpInside];
50     [self.view addSubview:btn];
51
52 }
53 #pragma mark - block传值、通知传值
54 //按钮关联方法
55 - (void)goToBack
56 {
57     [self dismissViewControllerAnimated:YES completion:nil];
58
59     //block传值
60     self.block(self.secondNotificationModel);
61
62     //通知传值
63     [[NSNotificationCenter defaultCenter] postNotificationName:@"passValueFromSecondVCToFirstVC" object:self.secondNotificationModel];
64 }
65
66 #pragma mark - 代理协议方法
67 - (void)passValueFromFirstVCToSecondVC:(DataModel *)dataModel
68 {
69     self.secondDelegateModel = dataModel;
70     self.label.text = [NSString stringWithFormat:@"name:%@ address:%@",self.secondDelegateModel.name,self.secondDelegateModel.address];
71 }
72
73 #pragma mark - 懒加载
74 //初始化_secondNotificationModel
75 - (DataModel *)secondNotificationModel
76 {
77     if (!_secondNotificationModel) {
78         _secondNotificationModel = [[DataModel alloc] init];
79         _secondNotificationModel.name = @"Bowen";
80         _secondNotificationModel.address = @"China";
81     }
82     return _secondNotificationModel;
83 }
84 //初始化self.block
85 - (void)setBlock:(Block)block
86 {
87     _block = block;
88 }
89
90
91 @end
时间: 2024-08-02 23:01:04

iOS UI-界面传值(三种方法)的相关文章

iOS拨打电话(三种方法)

iOS拨打电话(三种方法) 查了很多地方的关于iOS程序拨打电话,大都不全,今天我总结了三种方法,各有不同,拿来给大家分享,希望给大家有所帮助 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];    //            NSLog(@"s

用Fiddler可以设置浏览器的UA 和 手动 --Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!

附加以一种软件的方法是:用Fiddler可以设置浏览器的UA 以下3种方法是手动的 通过伪装User-Agent,将浏览器模拟成Android设备. 第一种方法:新建Chrome快捷方式 右击桌面上的Chrome浏览器图标,在弹出的右键菜单中选择“复制”,复制一个图标副本到桌面.右击该副本,选择“属性”,打开相应的对话框,在“目标”文本框的字符后面添加以下语句:“--user-agent="Android"”,如下图: 注意user前面是两个“-”,并且“chrome.exe”与“--

iOS图片拉伸的三种方法

方法一: iOS中有个叫端盖(end cap)的概念,用来指定图片中的哪一部分不用拉伸,上下左右不需要被拉伸的边缘就称为端盖. 1 // use resizableImageWithCapInsets: and capInsets. 3 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight __TVOS_PROHIBITED; 4 @pr

iOS 处理缓存的三种方法

缓存处理是个相当头疼的事情,要根据需要综合应用不同的策略.总的来说有以下几种情况: 1.URL缓存,例如社交应用的帖子浏览,要在viewDidAppear:里面进行URL缓存.简单来说就是用NSURLCache类,首先在AppDelegate.m里面的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:方法里面创建一个NSURLC

iOS 创建多线程的三种方法

<span style="font-size:18px;"><strong>(1)//通过NSObject的方法创建线程</strong></span> //(这个方法会自动开辟一个后台线程,参数1:在这个后台线程中执行的方法,参数2:用于传递参数) [self performSelectorInBackground:@selector(banZhuanPlus) withObject:nil]; (2)//通过NSThread创建线程(

iOS延时执行的三种方法

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); 1. NSTimer,可以设置

ios学习(界面传值的方法)

ios(学习)界面传值的方法 block: 实现界面传值的方法1.block: 实现界面传值,都是从第二个界面向第一个界面传值:第一种block 首先).在第二个界面secondViewController声明set方法 声明block @property (nonatomic,copy) void(^change)(UIColor *color); 其次).在.m文件实现 实现block的方法 _callback([UIColor redColor]);//注意这里之所以是_callback的

winform防止界面卡死的三种方法

在编程过程中经常会遇到耗时操作,这个时候如果不采取一些必要的异步操作,就会导致界面的卡死,这里以winform为例子,介绍三种方法防止界面卡死,对这几个方法稍加修改同样适用于wpf,silverlight等程序 首先给出一个函数模拟耗时操作 1使用委托+QueueUserWorkItem delegate void ChangeInvoke(int num) ; private void ChangeNum(int num) { MessageBox.Show(num.ToString());

iOS 拨打电话三种方法

小弟查了很多地方的关于iOS程序拨打电话,大都不全,今天我总结了三种方法,各有不同,拿来给大家分享,希望给大家有所帮助1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];// NSLog(@"str======%@",str);[[UIAppl

Android之intent传值的三种方法

Android之intent传值的三种方法分别是:1.startActivity():2.startActivityForResult():3.调用在下个activity自定义的方法"actionStart()",也就是最佳的方法. 一.一般方式传值跳转:startActivity()方法 /* 在MainActivity中定义如下代码 */ button1.setOnClickListener(new OnClickListener() { @Override public void