Detailed explanation of iOS page value transmission (forward transmission and reverse transmission)

  • 2021-10-11 19:42:31
  • OfStack

Proxy protocol value transfer

Sequential transmission

Assume that A is the first view controller and B is the second view controller

Import the. h file of B in A

Scenario: A to B

Step 1: Define an content attribute in. h of B


@interface SecondViewController : UIViewController
@property(nonatomic,copy)NSString *contents;
@end

Step 2: Assign a value to the content attribute of B by clicking the button in A


- (void)buttonAction:(UIButton *)button
 {
 NSLog(@" Enter the first 2 Page " ) ;
 SecondViewController *secondVC = [SecondViewController alloc] init];
 secondVC.contents = self.label.text;
 [self.navigationController pushViewController:secondVC animated:YES];
 }

Part 3: Using content properties to assign values to corresponding controls in B


@implemention SecondViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 self.view.backgroundColor = [UIColor whiteColor];
 self.navigationItem.title = self.contents;
 }

Back propagation

Proxy value passing is used to pass values from back to front after passing values in two interfaces.

Assume that A is the first view controller and B is the second view controller

Scenario: B to A

Step 1: First declare the protocol and protocol method in the. h file of B

Step 2: Declare a proxy attribute in. h of B. Here, we mainly pay attention to modifying it with assign or weak. weak and assign are pointers with non-ownership relationship. Pointer variables modified by these two modifiers will not change the reference count of referenced objects. However, weak automatically points the pointer to nil after 1 object is released, while assign does not. Therefore, it is safer to use weak.

@property (nonatomic,weak)id<协议名>delegate;


#pragma mark  This is B Adj. .h
#import<UIKit/UIKit.h>
@protocol CsutomTabBarDelegate<NSObject>
//  Put btn Adj. tag Outgoing method 
- (void)selectedIndexWithTag:(NSInteger)tag;
@end
@interface CustomTabBarView : UIView
// Declaration 1 Proxy attributes delegate
@property (nonatomic,weak)id<CsutomTabBarDelegate>delegate;
@end

Part 3: When B is about to return POP to the first interface, use protocol method to transfer data in the first line of pop method [self. delegate protocol method name: (parameter, that is, data to be returned)


#pragma mark  This is B Adj. .m
//  It is judged whether the protocol method is implemented in the formulated proxy class 
//  Ensure that this method does not crash when executed without it 
if([self.delegate respondsToSelector:@selector(selectedIndexWithTag:)])
{
 //  Execute proxy method 
 [self.delegate selectedIndexWithTag:(sender.tag - 1000)];
}
else
{
 NSLog(@" The method in the protocol is not implemented ");
}

In. m of A, before push to B interface methods, after initialization of B object, specify A object as proxy of B object (B object). delegate = self There is a yellow warning at this time because there is no quasi-compliance protocol


#pragma mark A Adj. .m Medium 
//  Specify the proxy, B Is customView
customView .delegate = self;

Step 5: Import the protocol name in the extension of A or the. h file of A < Agreement name >


#pragma mark A Adj. .m In the extension of, A Is RootTabBarController
//  Protocol import 
@interface RootTabBarController () <CustomTabBarDelegate>
@end

Step 6: In A. m event protocol method, obtain the parameters and present them on the current interface


#pragma mark A Adj. .m
//  Implement the proxy method, you can use the B The value that came in 
- (void)selectedIndexWithTag:(NSIngeter)tag
 {
  self.selectedIndex = tag;
 }

Use Block to transfer values between pages

Step 1: Redefine an block in. h of B, and declare an attribute of a class with this redefined block type. Note here that the block attribute is modified with copy


#pragma mark B Adj. .h 
#import <UIKit/UIKit.h> 
// block Value transfer  
//  Rename 1 With parameters and no return value block Type  
typedef void(^passValue)(NSInteger tag); 
@interface CustomTabBarView : UIView 
 // Use this block Type definition 1 Attributes  
@property (nonatomic,copy)passValue passValueTag; 
@end

Step 2: Call the method of block in the return method of. m of B


#pragma mark B Adj. .m In the return method of  
// Call block Method  
self.passValueTag(sender.tag - 1000);

Step 3: Assign a value to the block attribute of B where the instance of B is created in A. m. That is to say, write the content of this block, which is similar to assigning an initial value to a certain attribute of B


- (void)buttonAction:(UIButton *)button
 {
 NSLog(@" Enter the first 2 Page " ) ;
 SecondViewController *secondVC = [SecondViewController alloc] init];
 secondVC.contents = self.label.text;
 [self.navigationController pushViewController:secondVC animated:YES];
 }
0

Block memory that does not reference local variables is stored in the global area

Block memory that references local variables is stored in the stack area

The memory of Block exists in the heap area when copy operation is performed on Block

The Circular Reference Problem of Block

When Block is one attribute of self

self.circleBlock = ^(){my_self.navigationItem.title = @"Hello";};

Causes the reference count of self to +1, which eventually leads to circular references

Using weak to decorate variables under ARC to prevent circular references

Using block Modified Variables Under Non-ARC to Prevent Circular References


Related articles: