Value passing through an example in IOS

Source: Internet
Author: User

Value passing through an example in IOS

In IOS apps, different views and apps often share values and variables. There are several ways to achieve this:

1. extern Method

2. Singleton Mode

3. delegate Mode


The Singleton mode only has one instance as its name implies. It ensures that a class has only one instance and instantiate it and provides the instance to the entire system. It is often used for application-level shared resource control. This mode is frequently used. You can use a singleton class to transfer parameters between different views.


# Import
 
  
@ Interface Session: NSObject @ property (strong, nonatomic) NSString * singleValue; // The singleValue method + (Session *) GetInstance; @ end
 


# Import "Session. h "@ implementation Session // single instance object static Session * instance; // Single instance + (Session *) GetInstance {@ synchronized (self) {if (instance = nil) {instance = [[self alloc] init] ;}} return instance ;}- (id) init {if (self = [super init]) {self. singleValue = [[NSString alloc] init];} return self ;}@ end


Then, import the singleton class to use the singleton class.

Session * session = [Session GetInstance];

Session. singleValue = @ "Amazing! ";

NSString * value = session. singleValue;


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.