Single-case pass-through value

Source: Internet
Author: User

//

In iOS apps, there are often values and variable sharing in different view and applications, and there are several ways to do this:

1.extern mode

2. Single-case mode

3.delegate mode

The singleton pattern, as the name implies, is only one instance, which ensures that a class has only one instance, and instantiates it itself and provides it to the system as a whole. It is often used to do application-level shared resource control. This mode is used very frequently, and through a singleton class, you can implement parameter passing between different view

Build a model, inherit and NSObject, and create a singleton

#import <Foundation/Foundation.h>

@interface shareddata: nsobject

#pragma mark---- the way to create a single case

naming habits: share + class name

+ (instancetype) sharedata;

@property (nonatomic, strong)nsstring *inputnssting;

@end

#import "SharedData.h"

@implementation Shareddata

static shareddata *data = nil;

// Singleton class Use this method to create a singleton object

+ (instancetype) sharedata

{

if (data = = Nil) {

// If no object is created, it is yo high sharedata to Create a new object

Data = [[shareddata alloc] init];

}

// if created, returns the object created directly

return data;

}

@end

How to use

This is the assignment of the button binding event for a certain controller, and it is very simple to know which controller to assign the direct value to.

-(void) Button1action: (UIButton *) sender

{

// and then in the case where you need to use a singleton class import this singleton class

[shareddata sharedata]. Inputnssting = self. TextField. Text;

secondviewcontroller *SECONDVC = [[secondviewcontroller alloc] init];< /c9>

[self. Navigationcontroller pushviewcontroller: SECONDVC animated:YES];

   }

Single-case pass-through value

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.