当前位置: 首页 > 工具软件 > iOS Block UI > 使用案例 >

iOS开发 UI 正向传值 反向传值,Block传值

笪欣嘉
2023-12-01

正向传值

(属性传值)将A界面的值通过属性赋值给B界面

主要分三步:

准备工作:

建立两个控制器FirstViewcontroller,SecondViewcontroller,FirstView,SecondView

在FirstView中

#import <UIKit/UIKit.h>

@interface FirstView : UIView
@property (nonatomic,strong)UITextField *tf;
@property (nonatomic,strong)UIButton *button;
@end

#import "FirstView.h"
@implementation FirstView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.backgroundColor = [UIColor yellowColor];
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(100, 180, 100, 50);
    self.button.backgroundColor = [UIColor cyanColor];
    [self.button setTitle:@"跳转" forState:UIControlStateNormal];
    [self addSubview:self.button];
    
    self.tf = [[UITextField alloc] initWithFrame:CGRectMake(100, 80, 100, 50)];
    self.tf.borderStyle = UITextBorderStyleRoundedRect;
    self.tf.backgroundColor = [UIColor greenColor];
    [self addSubview:self.tf];
}

在SecondView

#import <UIKit/UIKit.h>
@interface SecondView : UIView
@property (nonatomic,strong)UIButton *button1;
@property (nonatomic,strong)UITextField *tf1;
@end

#import "SecondView.h"
@implementation SecondView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.backgroundColor = [UIColor orangeColor];
    
    self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button1.frame = CGRectMake(100, 200, 100, 50);
    self.button1.backgroundColor = [UIColor cyanColor];
    [self.button1 setTitle:@"返回" forState:UIControlStateNormal];
    [self addSubview:self.button1];
    
    self.tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
    self.tf1.backgroundColor = [UIColor yellowColor];
    self.tf1.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:self.tf1];
}

1.在目标控制器声明属性

#import <UIKit/UIKit.h>
#import "SecondView.h"
@interface SecondViewController : UIViewController
@property (nonatomic,strong)SecondView *sv;

#pragma mark     第一步:在目标的控制器中声明属性
@property (nonatomic,assign)NSString *text;

2.在push的时候设置属性

#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()

@end

@implementation FirstViewController
- (void)loadView{
    [super loadView];
    self.fv = [[FirstView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    self.view = self.fv;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self.fv.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonAction{
    SecondViewController *svc = [[SecondViewController alloc] init];
#pragma mark      第二步:在push的时候设置属性
    svc.text = self.fv.tf.text;
    [self.navigationController pushViewController:svc animated:YES];
}


3.在试图加载完成后赋值


#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
- (void)loadView{
    [super loadView];
    self.sv = [[SecondView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    self.view = self.sv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark          第三步:在试图加载完成后赋值
    
    self.sv.tf1.text = self.text;
    
    [self.sv.button1 addTarget:self action:@selector(button1Action) forControlEvents:UIControlEventTouchUpInside];

}

- (void)button1Action{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

反向传值

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


主要分五步:

准备工作:

建立两个控制器FirstViewcontroller,SecondViewcontroller,FirstView,SecondView

在FirstView中

#import <UIKit/UIKit.h>

@interface FirstView : UIView
@property (nonatomic,strong)UITextField *tf;
@property (nonatomic,strong)UIButton *button;
@end

#import "FirstView.h"
@implementation FirstView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.backgroundColor = [UIColor yellowColor];
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(100, 180, 100, 50);
    self.button.backgroundColor = [UIColor cyanColor];
    [self.button setTitle:@"跳转" forState:UIControlStateNormal];
    [self addSubview:self.button];
    
    self.tf = [[UITextField alloc] initWithFrame:CGRectMake(100, 80, 100, 50)];
    self.tf.borderStyle = UITextBorderStyleRoundedRect;
    self.tf.backgroundColor = [UIColor greenColor];
    [self addSubview:self.tf];
}

在SecondView

#import <UIKit/UIKit.h>
@interface SecondView : UIView
@property (nonatomic,strong)UIButton *button1;
@property (nonatomic,strong)UITextField *tf1;
@end

#import "SecondView.h"
@implementation SecondView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.backgroundColor = [UIColor orangeColor];
    
    self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button1.frame = CGRectMake(100, 200, 100, 50);
    self.button1.backgroundColor = [UIColor cyanColor];
    [self.button1 setTitle:@"返回" forState:UIControlStateNormal];
    [self addSubview:self.button1];
    
    self.tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
    self.tf1.backgroundColor = [UIColor yellowColor];
    self.tf1.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:self.tf1];
}


第一步,第二步都在在SecondViewController.h里面进行,申明协议,声明代理

#import <UIKit/UIKit.h>
#import "SecondView.h"
#pragma mark          第一步 声明协议

@protocol ValueDelegate <NSObject>

- (void)changeValue:(NSString *)value;

@end

@interface SecondViewController : UIViewController
@property (nonatomic,strong)SecondView *sv;

#pragma mark          第二步 声明代理
@property (nonatomic,assign)id<ValueDelegate>delegate;
@end

第三步在SecondViewController.m调用代理方法
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
- (void)loadView{
    [super loadView];
    self.sv = [[SecondView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    self.view = self.sv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self.sv.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    // Do any additional setup after loading the view.
}

- (void)buttonAction{
#pragma mark         第三步 调用代理方法
    [self.delegate changeValue:self.sv.tf.text];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

第四步第五步在FirstViewcontroller.m里面指定代理,实现协议的方法

#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()<ValueDelegate>

@end

@implementation FirstViewController
- (void)loadView{
    [super loadView];
    self.fv = [[FirstView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    self.view = self.fv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self.fv.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonAction{
    SecondViewController *svc = [[SecondViewController alloc] init];
#pragma mark       代理传值 第四步指定代理
    svc.delegate = self;//注:切记别忘了上面的代理
    [self.navigationController pushViewController:svc animated:YES];
}

#pragma mark           第五步 实现协议的方法
- (void)changeValue:(NSString *)value{
    self.fv.tf.text = value;
}


Block传值

主要分三步:

准备工作:

建立两个控制器FirstViewcontroller,SecondViewcontroller,FirstView,SecondView

在FirstView中

#import <UIKit/UIKit.h>

@interface FirstView : UIView
@property (nonatomic,strong)UITextField *tf;
@property (nonatomic,strong)UIButton *button;
@end

#import "FirstView.h"
@implementation FirstView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.backgroundColor = [UIColor yellowColor];
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(100, 180, 100, 50);
    self.button.backgroundColor = [UIColor cyanColor];
    [self.button setTitle:@"跳转" forState:UIControlStateNormal];
    [self addSubview:self.button];
    
    self.tf = [[UITextField alloc] initWithFrame:CGRectMake(100, 80, 100, 50)];
    self.tf.borderStyle = UITextBorderStyleRoundedRect;
    self.tf.backgroundColor = [UIColor greenColor];
    [self addSubview:self.tf];
}

在SecondView

#import <UIKit/UIKit.h>
@interface SecondView : UIView
@property (nonatomic,strong)UIButton *button1;
@property (nonatomic,strong)UITextField *tf1;
@end

#import "SecondView.h"
@implementation SecondView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.backgroundColor = [UIColor orangeColor];
    
    self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button1.frame = CGRectMake(100, 200, 100, 50);
    self.button1.backgroundColor = [UIColor cyanColor];
    [self.button1 setTitle:@"返回" forState:UIControlStateNormal];
    [self addSubview:self.button1];
    
    self.tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
    self.tf1.backgroundColor = [UIColor yellowColor];
    self.tf1.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:self.tf1];
}
第一步:Block回传第一步 创建一个block属性

#import <UIKit/UIKit.h>
#import "SecondView.h"
#pragma mark        Block回传第一步 创建一个block属性
typedef void(^valueBlock) (NSString *value);
typedef void(^loopBlock)();
@interface SecondViewController : UIViewController
@property (nonatomic,strong)SecondView *sv;
@property (nonatomic,copy)valueBlock block;//block用copy,复制到堆区
@property (nonatomic,copy)loopBlock LB;
@end

第二步:   Block回传第二步 在返回的时候调用block属性

#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)loadView{
    [super loadView];
    self.sv = [[SecondView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    self.view = self.sv;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.sv.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
//解决block的死循环
    //两个充分必要条件1.对象包含一个block属性 2.这个block属性中引用了self
    //解决方法:__block
    //__block 是在MRC下的,ARC用__weak
    //__weak SecondViewController *weakSelf = self;
    __weak typeof (self) weakSelf = self;//意思和上面的一样
    self.LB = ^(){
        weakSelf.sv.backgroundColor = [UIColor redColor];
    };
}

- (void)buttonAction{
    [self.navigationController popToRootViewControllerAnimated:YES];
#pragma mark        Block回传第二步 在返回的时候调用block属性
    self.block(self.sv.field.text);
}

第三步:Block回传第三步 给第二个页面的block属性赋值

#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()

@end

@implementation FirstViewController
- (void)loadView{
    [super loadView];
    self.fv = [[FirstView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    self.view = self.fv;
   }
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.fv.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonAction{
    SecondViewController *svc = [[SecondViewController alloc] init];
#pragma mark        Block回传第三步 给第二个页面的block属性赋值
    svc.block = ^(NSString *value){
        self.fv.field.text = value;
    };
    [self.navigationController pushViewController:svc animated:YES];
}


 类似资料: