IOS Pass-Through value summary

Source: Internet
Author: User

In the development of iOS, we often need to use the value of data transfer and loading, whether it is a cross-page value or not across the page value, the implementation of the value of the transfer I simply did the following summary:

One: Use notification (nsnotificationcenter) to pass the value:

First we jump from Viewcontroller to Rootviewcontroller and write the following code in Rootviewcontroller:


Then in the Viewcontroller to receive this notice, must be in the main thread inside such as: viewdidload function inside Add this code:


This allows us to simply use the notification to achieve the value of the pass.Second: The value of the transfer is achieved by delegating delegate:again we jump from Viewcontroller to Rootviewcontroller and write the following code in ROOTVIEWCONTROLLER.H:

the proxy is set with the weak property to prevent circular references, and then Add the following code to the ROOTVIEWCONTROLLER.M:

finally back to our ViewController.h file, refer to the RootViewController.h header file and follow the protocol, such as:


In the viewcontroller.m file, implement the Proxy method


So we have implemented the method of proxy value, is it very simple?

Three: Using block to achieve the value:

As with the first step above, we first jump from Viewcontroller to Rootviewcontroller, declare block in RootViewController.h and write the following code:

Then create this block in rootviewcontroller.m, such as:

Finally we implement the callback to block in the method of VIEWCONTROLLER.M jump, such as:


This allows us to implement the process of using block to achieve the value of the pass.

Third: The use of nsuserdefault to achieve value,Nsuserdefault can save a small amount of data in the sandbox, we can store the data in a sandbox, and then in other places by reading the sandbox, to get the data to achieve the transfer of data, Generally, we seldom use this method. The simple steps are as follows:

Then add the following code in the VIEWCONTROLLER.M to achieve the value-added effect:

four: Using a single example to achieve the value:

First we need to create a singleton, for example: MyName object, in MyName.h, as follows:

Then implement the + (MyName *) Shareddataname method in MYNAME.M, such as:

Then implement the following methods in ROOTVIEWCONTROLLER.M:

Finally, the following code is added to the VIEWCONTROLLER.M to enable the use of single-pass values:

Five: The use of attribute transfer value, this is the simplest method of transmission, the general novice with a lot of, simply speaking, after the adjustment method of the object is initialized directly to get this value to the object of a property assignment, as follows:

However, the value of attribute transfer is more limited and less used. Temporarily summarize these, of course, perhaps there may be a better way to pass the value, if you know, then please leave a message, it must be grateful, if there is a bad place to write, welcome to correct, but also hope to communicate with each other, common progress! Thank you for watching! If reproduced, please attach the link, thank you!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS Pass-Through value summary

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.