Direct/multi-level data transmission between ios Interfaces

Source: Internet
Author: User

Direct/multi-level data transmission between ios Interfaces
1. Initiate value transfer

(Rewrite the init method to include the data to be passed in later, and then initialize the interface. to use this method, you must have data before initialization)

# Import <UIKit/UIKit. h> @ interface CustomView: UIView // note that (. h) Declare the method before creating this class externally.-(instancetype) initWithFrame :( CGRect) frame withInformation :( NSDictionary *) dict; @ end

# Import "CustomView. h "@ implementation CustomView-(instancetype) initWithFrame :( CGRect) frame withInformation :( NSDictionary *) dict {self = [super initWithFrame: frame]; if (self) {// configure the page to directly obtain the desired data through the dictionary} return self;} @ end


-(Void) initUserInterface {NSDictionary * dict = [NSDictionary dictionary]; // call the init custom method to input data CustomView * customView = [[CustomView alloc] initWithFrame: self. view. bounds withInformation: dict]; [self. view addSubview: customView];}



2. Attribute Value passing

(If you want to update the content of elements on the interface, it is convenient to directly obtain the corresponding element through the attribute. In particular, when tableView is used to display data requests, the request data is synchronized with the page creation. Therefore, if the request data is transmitted using init, it must be empty. Therefore, the data request must be used successfully, refresh the interface elements, while the cell is also customized. You 'd better define the control to be updated as an attribute to refresh the data)

#import <UIKit/UIKit.h>@interface CustomView : UIView@property (nonatomic,retain)UILabel *label;@end

# Import "CustomView. h "@ implementation CustomView-(instancetype) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {_ label = [[UILabel alloc] init]; _ label. text = @ "attribute pass value"; [self addSubview: _ label];} return self ;}@ end

# Import "ViewController. h "# import" CustomView. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self initUserInterface];}-(void) initUserInterface {CustomView * customView = [[CustomView alloc] initWithFrame: self. view. bounds]; // obtain the attributes of customView. label. text = @ "Modify display data"; [self. view addSubview: customView];} @ end


3. Pass the value of the method parameter

(The pass-through of method parameters is also suitable for updating interface elements. When you want to refresh the data returned by a encapsulated TableView request, you can refresh the interface data by calling the encapsulated method)

4. Protocol value transfer

(It is more suitable for direct reverse transfer of values between two interfaces __> is also a proxy method similar to the implementation system. When a encapsulated class defines its own proxy, when an event triggered in this class needs to transmit data, define a method in the Protocol. When an instance that complies with this Protocol calls this method, it can access the parameters carried later)

The following is a simple encapsulation of a view to illustrate the encapsulated. h. m file.

# Import <UIKit/UIKit. h> @ protocol CustomViewDelegate <NSObject> // method required for delegate @ required-(void) sendInformation :( NSInteger) tag; // method for implementing the delegate selection @ optional @ end @ interface CustomView: UIView @ property (nonatomic, assign) id <CustomViewDelegate> delegate; @ end


#import "CustomView.h"@implementation CustomView- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];        button.tag = 111;        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];        [self addSubview:button];    }    return self;}- (void)buttonPressed:(UIButton *)sender{    [self.delegate sendInformation:sender.tag];}@end

VC on the home page

# Import "ViewController. h "# import" CustomView. h "@ interface ViewController () <CustomViewDelegate> @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self initUserInterface];}-(void) initUserInterface {CustomView * customView = [[CustomView alloc] initWithFrame: self. view. bounds]; // create an instance and send it to the proxy customView. delegate = self; [self. view addSubview: customView] ;}# pragma mark-CustomViewDelegate-(void) sendInformation :( NSInteger) tag {// this method is called when a button event is triggered, the data is passed over. Compared to the row clicked by tableView, NSLog (@ "% ld", tag);}-(void) is used as the proxy) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ end


5 Block Value passing

 

6. Pass values in a single example

(The user information is stored in a single instance for convenient data access or other data calls at any time)

7. Notification transfer value

(When you need to transfer data on a multi-level page, it is convenient to register a notification, and it is also very simple to use)


# Import "ViewController. h "# import" CustomView. h "@ interface ViewController ()-(void) getInformation :( NSNotification *) noti; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self initUserInterface];} -(void) initUserInterface {[[nsicationcenter center defaultCenter] addObserver: self selector: @ selector (getInformation :) name: @ "sendData" object: nil];} # pragma mark-NSNotificationCenter methods // after the notification is sent, this method is used-(void) getInformation :( NSNotification *) noti {/***************** communications ****************//*@ interface NSNotification: NSObject <NSCopying, NSCoding> @ property (readonly, copy) NSString * name; @ property (readonly, retain) id object; @ property (readonly, copy) NSDictionary * userInfo; * /// print the transmitted data to NSLog (@ "% @", noti. object); // you can directly obtain the transmitted data using the vertex attribute.} @ end



# Import "CustomView. h "@ implementation CustomView-(instancetype) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {UIButton * button = [UIButton buttonWithType: Success]; [button addTarget: self action: @ selector (postNoti) forControlEvents: UIControlEventTouchUpInside]; [self addSubview: button];} return self;} // click the button to send a notification-(void) postNoti {NSString * string = @ "send any data"; [[nsicationcenter center defacenter center] postNotificationName: @ "sendData" object: string]; // if the object is ID, any type of data can be transferred. The string is passed here} @ end


8 extern global variables


9 Data Storage

(Data is written to the sandbox persistently)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.