IOS development page pass value

IOS development page pass value

preface

This article will basically introduce some methods of page value transfer in IOS, the language is based on Objective-c

  1. Attribute-by-value
  2. Proxy pass
  3. Block pass value
  4. Simple interest pass

1. Attribute passing

After the value of page A jumps to page B, the value is transferred to page B. You only need to define the attributes in the .h file, and then assign the attribute of the instance object of page B before the page A jumps. This method of value transfer is generally used to forward the value to the jump interface.

A page .m file

#import "ViewController.h"
#import "ViewController2.h"

@interface ViewController ()
{
  	//此处要定义成全局的控件,因为B页面也要用
    UITextField *tf;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    tf = [[UITextField alloc] initWithFrame:CGRectMake(70, 200, 280, 40)];
    tf.backgroundColor = [UIColor grayColor];
    [self.view addSubview:tf];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(160, 260, 100, 30);
    [btn setTitle:@"传值" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

- (void) jump{
    ViewController2 *v2 = [[ViewController2 alloc] init];
    
    //在跳转前进行传值
    v2.str = tf.text;
    
    [self presentViewController:v2 animated:YES completion:nil];
}
@end

Page B.h file

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ViewController2 : UIViewController
//声明一个属性用来接收要传过来的值,这里定义为传过来一个字符串
@property(nonatomic,strong) NSString *str;
@end

NS_ASSUME_NONNULL_END

B page .m file

#import "ViewController2.h"

@interface ViewController2 ()


@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(70, 200, 280, 40)];
    lab.backgroundColor = [UIColor grayColor];
  
    //lab的值就是传过来的字符串的值
    lab.text = self.str;
    
    [self.view addSubview:lab];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake(160, 260, 100, 30);
    [btn2 setTitle:@"返回" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
}

- (void) back {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

2. Proxy pass value

Attribute transfer is used to transfer the value in the forward direction. When we need to transfer the value in the reverse direction, use the proxy to transfer the value.

We pass the value to be passed back as a parameter in the proxy method.

We are writing the agreement on the second page and using it on the first page

The following is the operation of writing the protocol (the following are all done in the .h file of the B page)

  • 1. Statement of Agreement

  • 2. Declaring attributes (agent)

  • 3. Declare proxy method

.H file of page B

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

//1.声明协议
@protocol passValueDelegate <NSObject>
//3.实现方法
- (void)passColor:(id) color;
@end

@interface ViewController2 : UIViewController

//2.声明属性(代理人)
//默认用assign 和 id
@property(nonatomic,assign) id <passValueDelegate> delegate;

@end

NS_ASSUME_NONNULL_END

Next is the operation to implement the protocol (the following operations are all completed in the .m file on page A)

  • Comply with the agreement
  • Appointed agent
  • Write proxy method

.M file of page A

#import "ViewController.h"
#import "ViewController2.h"

//遵守协议
@interface ViewController () <passValueDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(160, 260, 100, 30);
    [btn setTitle:@"传值" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

- (void) jump{
    ViewController2 *v2 = [[ViewController2 alloc] init];
    
    //指定代理人
    v2.delegate = self;
    [self presentViewController:v2 animated:YES completion:nil];
}

//写代理方法
- (void) passColor:(id)color {
    self.view.backgroundColor = color;
}

@end

Proxy method is triggered when page B returns to page A

.M file of page B

#import "ViewController2.h"

@interface ViewController2 ()
@end
  
@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
  
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake(160, 260, 100, 30);
    [btn2 setTitle:@"返回" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
}

- (void) back {
    //返回的时候触发代理方法
    [self.delegate passColor:self.view.backgroundColor];
    [self dismissViewControllerAnimated:YES completion:nil];
}

3.Block pass value

Block callback pass value is usually used for reverse pass value between two pages. The A page needs to receive the value from the B page, that is, the backward pass value. Here we use the Block callback.

The following are the basic operations using block

  • statement

Declare block attribute in page B

#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SecondViewController : UIViewController
//用copy将block从栈区拷贝到堆区里面去
@property(nonatomic,copy) void(^myBlock)(NSString *str);
@end
NS_ASSUME_NONNULL_END
  • definition

In the A page, get the B page object, and use this object to write the implementation of the block

#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()
{
    UILabel *lab;
}

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(Next)];
    
    lab = [[UILabel alloc] initWithFrame:CGRectMake(70, 200, 280, 40)];
    lab.backgroundColor = [UIColor grayColor];
    lab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lab];
}

- (void)Next {
    SecondViewController *v2 = [[SecondViewController alloc] init];
    //block的实现
    v2.myBlock = ^(NSString * _Nonnull str) {
        self->lab.text = str;
    };
    [self.navigationController pushViewController:v2 animated:YES];
}

@end

Note: This is just to define the Block method. When the user clicks the "jump button", the code inside the Block will not be executed. The code inside the Block will only be executed when the Block is called.

In this example, the code in the block will only be called when "return" is clicked on the second page

  • transfer

Call Block in B page to pass value

#import "SecondViewController.h"

@interface SecondViewController ()

@end
@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(Back)];
}
- (void) Back{
    //调用
    self.myBlock(@"block传值");
    [self.navigationController popViewControllerAnimated:YES];
}

@end

For some basic related knowledge about block, you can check my other blog.

4. Inform the center to pass the value

The notification center transmits values, which can be transmitted across multiple pages, generally from the back page to the front page. (One-to-many process)

For example, when a radio station starts playing a program, the frequency of the radio station is the value passed. When our users want to listen to this program, we have to turn on the radio and then tune to the corresponding channel to listen to the program. In this process, the broadcasting station is only responsible for sending, regardless of who will receive it.

The page receiving the message is the radio , and the registration notification center is to turn on the radio and FM

The page that sends the message is the broadcast station , and the broadcast is sent when the page jumps

The following is the basic operation of the notification center to pass the value

  • Create a notification center on page A, and register a monitoring event through the notification center
  • On page A, set the event that receives the notification
  • Remove notification center on page A
  • In the B page, establish a notification center, through the notification center, send a notification (that is, the process of transferring values), and transfer the value to be transmitted as the object to the first interface
//监听通知
// 通知中心是一个单例,它会将通知广播出去,内存中的所有对象都可以通过该通知中心来监听其他对象或者是自己所发出的通知
    // 第一个参数:观察者(即谁收到这个通知将会做出响应)
    // 第二个参数:观察者收到通知以后将会调用哪个方法
    // 第三个参数:通知的名称
    // 第四个参数:监听谁发来的通知,nil表示所有对象发送的通知都接收
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countAdded) name:@"countAdded" object:nil];

// 发送通知
    // 第一个参数:通知名称
    // 第二个参数:通知的发送者
    // 第三个参数:一个字典,随着通知一起传递的信息
    [[NSNotificationCenter defaultCenter] postNotificationName:@"countAdded" object:self userInfo:userInfo];

** Register the notification center on page A.m file, and execute the notification method, and finally remove the notification center

#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()
{
    UILabel *lab;
}

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(Next)];
    
    lab = [[UILabel alloc] initWithFrame:CGRectMake(70, 200, 280, 40)];
    lab.backgroundColor = [UIColor grayColor];
    lab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lab];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveMessage:) name:@"90.8" object:nil];
}


//收到通知执行的方法
- (void)receiveMessage:(NSNotification *)mes{
    //从通知里面取出值
  	//传的值需要转换成字符串
    lab.text = [NSString stringWithFormat:@"%@",mes.object];
}

- (void)Next {
    SecondViewController *v2 = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:v2 animated:YES];
}

//移除当前对象监听的事件
- (void)dealloc
{
  	//这是删除所有的通知中心,也可以按照属性来删除
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

Send notification in .m file on page B

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(Back)];
}
- (void) Back{
    
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"90.8" object:@"通知传值" userInfo:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

@end

The message notification center will make the code very messy when it is used, but it can pass values ​​across multiple pages.

The above are some basic operations on the page pass value

Guess you like

Origin www.cnblogs.com/mgd666/p/12704201.html
Recommended