iOS - lazy loading and block value transfer

Posted by anand_ragav on Sun, 02 Jan 2022 22:36:35 +0100

Lazy loading

First, before learning lazy loading, we must know what lazy loading is. Lazy loading is a method that we usually use to store the array of network data, the view of the controller, the customization of controls, complex operation logic and so on. Generally speaking, it is loaded when we need it.

Advantages of lazy loading

  1. It is also called delayed loading, which can be loaded when it is used
  2. Once loaded, it will not be loaded again, saving system resources
  3. Lazy loading can also solve some sequence problems that may be encountered in actual development
  4. Small running memory, saving memory

Lazy loading idea

  1. Create a property in the class extension
  2. Rewrite the getter corresponding to this attribute and put the logic to be implemented into this getter
  3. Considering that lazy loading is only loaded once, you should judge whether this attribute is empty before implementing the logic. If it is empty, the logic will be executed. Otherwise, this attribute will be returned directly

Lazy loading usage

Xianming UILabel:

@property(nonatomic , strong)UILabel* label;

Lazy loading implementation:

-(UILabel *)label{
	if (_label == nil) { //Judge whether an instance exists. If it does not exist, create an instance
	_label = [[UILabel alloc]init];//Create instance
return _label;


Pay attention to the use_ Self. Cannot be used for label Label, because the getter method will be called in a loop, resulting in an endless loop.

block pass value

When we use block to transfer values, we must pay attention to two points:

  1. First, you must declare a block on the second page
  2. When the first page jumps to the second page, assign a value to the attribute in the block to complete the value transfer of the block.


First, on the second page, secondviewcontroller Define a block in H:

//Define Block
typedef void (^PushBlock)(NSString *);

@interface SecondViewController : UIViewController
@property(nonatomic , strong)PushBlock pushValueString;
@property(nonatomic , strong)UITextField* textField;
@property(nonatomic , strong)NSString* textString;
@property(nonatomic , strong)UIButton* button;

Then in the secondviewcontroller Set the text box and button in M, similarly in viewcontroller Text boxes and buttons are also set in M:

 [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 250, self.view.bounds.size.width, 50)];
    _textField.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_textField];
    _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _button.frame = CGRectMake(20, 350, self.view.bounds.size.width-40, 50);
    _button.backgroundColor = [UIColor purpleColor];
    _button.titleLabel.font = [UIFont systemFontOfSize:25.0];
    _button.tintColor = [UIColor whiteColor];
    [_button setTitle:@"Back" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(pressback:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];

We also want to complete the click event corresponding to the button:

- (void)pressback: (UIButton*)sender {
    //    Core code
    _pushValueString (_textField.text);
    [self dismissViewControllerAnimated:YES completion:nil];

In viewcontroller Button events in M:

- (void)pressnext:(UIButton*)sender {
    SecondViewController* second = [[SecondViewController alloc] init];
    second.pushValueString = ^(NSString* string) {
        _textFieldFirst.text = string;
    second.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:second animated:YES completion:nil];

This completes the process of first declaring a block on the second page, assigning values to the attributes in the block when the first page jumps to the second page, and completing the value transfer process of the block.


Topics: objective-c