Single-case data transfer

Source: Internet
Author: User


Single case transfer value ------- if there are many different pages, you need to pass the value, save the value to a third party, set the third party to the singleton Mode

The Code is as follows:

<pre name="code" class="objc">#import <Foundation/Foundation.h>@interface Singleton : NSObject+ (Singleton *)shareSingleton;@property (nonatomic,copy)NSString * text;@end


 

#import "Singleton.h"@implementation Singletonstatic Singleton * singleton = nil;+ (Singleton *)shareSingleton{    @synchronized(self){        if (singleton == nil) {            singleton = [[Singleton alloc]init];        }    }    return singleton;}@end


# Import "firstviewcontroller. H "# import" secondviewcontroller. H "# import" uibutton + create. H "# import" Singleton. H "@ interface firstviewcontroller () {uitextfield * _ textfield; // create an input box} @ end @ implementation firstviewcontroller-(void) dealloc {[_ textfield release]; [Super dealloc];}-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: callback]; If (Self) {// initim initialization} return self;}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor redcolor]; self. navigationitem. title = @ "Homepage";/*** 1. create an input box **/_ textfield = [[uitextfield alloc] initwithframe: cgrectmake (50, 80,200, 30)]; _ textfield. borderstyle = uitextborderstyleroundedrect; [self. view addsubview: _ textfield];/*** 1. create a uibutton, * 2. add a response event to jump from the home page to the second page. */uibutton * button = [uibutton systembuttonwithframe: cgrectmake (100,120, 50, 50) Title: @ "push" target: Self action: @ selector (didclickbuttonaction)]; [self. view addsubview: button]; // do any additional setup after loading the view .} -(void) didclickbuttonaction {/*** 1. use the push method to launch the next page * 2. input the string in the homepage input box to receive the string in the singleton mode. in this way, the strings entered in the homepage input box are uploaded to the uilabel on the second page. */secondviewcontroller * secondvc = [[secondviewcontroller alloc] init]; [Singleton sharesingleton]. TEXT = _ textfield. text; [self. navigationcontroller pushviewcontroller: secondvc animated: Yes]; [secondvc release];}-(void) didreceivemorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end



# Import "secondviewcontroller. H "# import" Singleton. H "@ interface secondviewcontroller () @ end @ implementation secondviewcontroller-(void) dealloc {[_ label release]; [Super dealloc];}-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization} return self;}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor orangecolor]; self. navigationitem. title = @ "second page";/*** 1. create a uilabel * 2 in the second interface. receives the string entered in the homepage input box through the single-instance class attribute nsstring * text * 3. then assign a value to uilabel */_ label = [[uilabel alloc] initwithframe: cgrectmake (50, 80,200, 30)]; _ label. backgroundcolor = [uicolor greencolor]; _ label. TEXT = [Singleton sharesingleton]. text; [self. view addsubview: _ label]; // do any additional setup after loading the view .} -(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} @ end


Single-case data transfer

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.