zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    代码示例_触摸屏驱动
    代码示例_中断下半部
    代码示例_mmap的实现
    代码示例_阻塞IO
    代码示例_LCD控制
    代码示例_平台总线
    驱动_I2c驱动框架
    驱动_Input输入子系统
    Windows切换桌面或窗口快捷键
    几何分布
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5113925.html
Copyright © 2011-2022 走看看