Summary of the methods for transferring values between iOS pages, and summary on the ios page
If you don't talk nonsense, go to the topic:
There are 6 methods to talk about here: 1. attribute value transfer 2, block 3, delegate 4, UserDefault 5, Singleton 6, and notification (I only write the core code for the reason of length, if you cannot understand it, go to github to view the demo at the bottom)
1. block (I personally think it is best to use it in the most common scenarios)
Let's talk about the most commonly used block. attribute value transfer is very simple and mainly used for downstream data transfer. Here we will discuss reverse data transfer as follows. Attribute values are written together in the block.
The code below:
1 // secondVc. h2 typedef void (^ TestBlock) (NSString * str); 3 4 @ interface ATNextViewController: UIViewController5 // define block6 @ property (nonatomic, copy) TestBlock testBlock; 7 @ end
1 // secondVc. m2-(void) btnClick :( UIButton *) btn {3 if (self. testBlock) {// The block value is 4 self. testBlock (@ "green"); 5} 6 7 [self. navigationController popViewControllerAnimated: YES]; 8}
The following is the first VC code:
// Vc. m-(void) btnClick :( UIButton *) btn {ATNextViewController * nextVc = [[ATNextViewController alloc] init]; nextVc. inStr = @ "red"; // attribute to pass the value, which is used for downstream transmission. You can directly transfer it. // here is the value returned by block nextVc. testBlock = ^ (NSString * str) {NSLog (@ "% @", str) ;}; [self. navigationController pushViewController: nextVc animated: YES];}
2. delegate
The proxy first needs to figure out who transfers the value to, and here I mainly write about inverse transmission. The inner controller transmits the value to the outer controller, that is, secondVc transfers the value to Vc, therefore, Vc should act as the proxy of secondVc and receive the value passed by secondVc in the proxy method.
SecondVc code:
1 // secondVc. h 2 3 // declare proxy 4 @ protocol ATNextVcDelegate <NSObject> 5 6 @ optional 7-(void) inStr :( NSString *) inStr; 8 @ end 9 @ interface ATNextViewController: UIViewController10 @ property (nonatomic, weak) id <ATNextVcDelegate> delegate; 11 @ end
1 // secondVc. m2 // The proxy value is 3 4-(void) btnClick :( UIButton *) btn {5 if ([self. delegate respondsToSelector: @ selector (inStr :)]) {6 [self. delegate inStr: @ "red"]; 7} 8 [self. navigationController popViewControllerAnimated: YES]; 9}
Vc code:
1 // Vc. m 2 // The value 3 4-(void) btnClick (UIButton *) btn {5 ATNextViewController * nextVc = [[ATNextViewController alloc] init]; 6 nextVc. delegate = self; 7 [self. navigationController pushViewController: nextVc animated: YES]; 8} 9 # pragma mark-ATNextVcDelegate10-(void) inStr :( NSString *) inStr {11 NSLog (@ "% ========", inStr); 12}
3. UserDefault
This method saves files locally, which is a type of data persistence (a detailed explanation of data persistence is provided if you have time. Opening the program again will still have the value left by the last save.
Here there are only two actions:
1 // secondVc. m 2-(void) btnClick :( UIButton *) btn {3 // save 4 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 5 [defaults setObject: @ "yellow" forKey: @ "btnColor"]; 6 [defaults setObject: @ "100x100" forKey: @ "btnSize"]; 7 [defaults synchronize]; 8 9 [self. navigationController popViewControllerAnimated: YES]; 10 11}
1 // Vc. m 2 3-(void) viewWillAppear :( BOOL) animated {4 [super viewWillAppear: animated]; 5 // obtain 6 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 7 NSString * btnColor = [defaults objectForKey: @ "btnColor"]; 8 NSString * btnSize = [defaults objectForKey: @ "btnSize"]; 9 NSLog (@ "% @", btnColor, btnSize); 10}
Different from the previous one, This method takes a value as long as you enter this interface, not necessarily only returned from the previous interface.
4. Singleton
A singleton is generally used as a macro directly, and it will be used directly when it is used. Therefore, the singleton macro file will not be written here. If you are interested, you can go to the demo and check the Code directly:
Here I have defined a singleton class that stores user login status information, followed by the use in the controller:
//ATLoginStatus.h#import <Foundation/Foundation.h>#import "ATSingleton.h"@interface ATLoginStatus : NSObject@property (nonatomic, assign) BOOL isLogin;@property (nonatomic, copy) NSString *phoneNumStr;SingletonH(LoginStatus)@end
// ATLoginStatus.m#import "ATLoginStatus.h"@implementation ATLoginStatusSingletonM(LoginStatus)@end
// SecondVc. m-(void) btnClick :( UIButton *) btn {// storage ATLoginStatus * status = [ATLoginStatus sharedLoginStatus]; status. phoneNumStr = @ "12345"; status. isLogin = YES; [self. navigationController popViewControllerAnimated: YES];}
// Vc. m-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated]; // retrieve ATLoginStatus * status = [ATLoginStatus sharedLoginStatus]; NSLog (@ "% @ % d ", status. phoneNumStr, status. isLogin );}
5. Notifications
Notifications are sent and received in two ways:
Send notification:
1 // secondVc. m2 3-(void) btnClick :( UIButton *) btn {4 // send a notification 5 [[NSNotificationCenter defacenter center] postNotificationName: @ "btnColorNoti" object: self userInfo: @ {@ "btnColor": @ "yellow"}]; 6 [self. navigationController popViewControllerAnimated: NO]; 7}
Receive notification:
// Vc. m-(void) viewDidLoad {[super viewDidLoad]; //... // register the notification [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (btnColorNoti :) name: @ "btnColorNoti" object: nil];} // call this method when receiving the notification to receive the notification message-(void) btnColorNoti :( NSNotification *) noti {NSDictionary * dict = noti. userInfo; NSLog (@ "% @", dict [@ "btnColor"]);} // cancel notification listening when the controller is destroyed-(void) dealloc {[[NSNotificationCenter defacenter center] removeObserver: self];}
OK, that's all. Although these methods can be used to pass values between controllers, but in fact, some methods do not need to be used in the common scenario I used for example (I will understand what the scenario is after running my demo) (of course, attribute values can only be used for downstream transmission ), in the scenario I used as an example, block is the most concise, and delegate is also acceptable. However, I personally feel that it is too troublesome to transmit values, and the notification is also acceptable, but it is not as good as block. However, singleton and UserDefaults do not need to be used in this scenario. They can be used when two controllers are far away, or when two controllers are not associated. Just like in the example of a single instance, you can save the user information that may be used globally, so that the information can be obtained at any time.
The following is a demo:
My github: https://github.com/alan12138/somethingInteresting