Use Sharedapplication for value transfer

Source: Internet
Author: User

In general, there are four common ways to pass values between pages in iOS,

1 use sharedapplication to define a variable to pass .

2 Use file plist, or nsuserdefault to pass

3 pass through a single instance of class

4 pass through Delegate .

I'll start with the first approach, and here's an example:

(1) AppDelegate.h

#import <UIKit/UIKit.h> @interface appdelegate:uiresponder <UIApplicationDelegate> @property (Strong, nonatomic) UIWindow *window; @property (Strong, nonatomic) NSString *dname;//define Dname pass-through account value @property (strong, Nonatomic) NSString *dpass;//defines the value of Dpass Pass-through password @end

Appdelegate.m

#import "AppDelegate.h" #import "ViewController.h" @interface appdelegate () @end @implementation appdelegate-(BOOL) Application: (uiapplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {    // Create the window    Self.window = [[UIWindow alloc]initwithframe:[[uiscreen mainscreen] bounds];    Self.window.backgroundColor = [Uicolor whitecolor];    Self.window.rootViewController = [[Viewcontroller alloc]init];    [Self.window makekeyandvisible];     return YES;} @end


(2) ViewController.h

#import <UIKit/UIKit.h> #import "AppDelegate.h" @interface viewcontroller:uiviewcontroller@end

Viewcontroller.m

#import "ViewController.h" #import "NavViewController.h" uitextfield* Nametextfield; Uitextfield *passtextfield, @interface viewcontroller () @end @implementation viewcontroller-(void) Viewdidload {[Super    Viewdidload];    uilabel* Namelabel = [[UILabel alloc]initwithframe:cgrectmake (30, 50, 50, 30)];    Namelabel.text = @ "Account";        [Self.view Addsubview:namelabel];    uilabel* Passlabel = [[UILabel alloc]initwithframe:cgrectmake (30, 100, 50, 30)];    Passlabel.text = @ "password";        [Self.view Addsubview:passlabel];    Nametextfield = [[Uitextfield alloc]initwithframe:cgrectmake (100, 50, 150, 30)];    Nametextfield.borderstyle = Uitextborderstyleroundedrect;        [Self.view Addsubview:nametextfield];    Passtextfield = [[Uitextfield alloc]initwithframe:cgrectmake (100, 100, 150, 30)];    Passtextfield.borderstyle = Uitextborderstyleroundedrect;        [Self.view Addsubview:passtextfield];    uibutton* loginbtn = [UIButton Buttonwithtype:uibuttontypesystem]; Loginbtn.frame = CgrectmaKe (60, 180, 72, 30);    [Loginbtn settitle:@ "Landing" forstate:uicontrolstatenormal];    [Self.view ADDSUBVIEW:LOGINBTN];        [Loginbtn addtarget:self Action: @selector (Onlogin:) forcontrolevents:uicontroleventtouchupinside];    }-(void) Onlogin: (ID) sender{appdelegate *appdelegate = (appdelegate *) [[UIApplication sharedapplication]delegate];    Appdelegate.dname = Nametextfield.text;;        Appdelegate.dpass = Passtextfield.text;        navviewcontroller* navctr = [[Navviewcontroller alloc] init];   [Self presentviewcontroller:navctr animated:yes completion:nil]; } @end

(3) NavViewController.h

#import <UIKit/UIKit.h> #import "AppDelegate.h" @interface navviewcontroller:uiviewcontroller@end

Navviewcontroller.m

#import "NavViewController.h" uilabel* WelcomeLabel, @interface navviewcontroller () @end @implementation    navviewcontroller-(void) viewdidload {[Super viewdidload];    Appdelegate *appdelegate = (appdelegate *) [[uiapplication sharedapplication] delegate];    uilabel* Hellolabel = [[UILabel alloc]initwithframe:cgrectmake (30, 50, 150, 30)];    Hellolabel.text = @ "Welcome,";                [Self.view Addsubview:hellolabel];    uilabel* Passlabel = [[UILabel alloc]initwithframe:cgrectmake (30, 100, 150, 30)];    Passlabel.text = @ "Your password is,";        [Self.view Addsubview:passlabel];    uilabel* WelcomeLabel = [[UILabel alloc]initwithframe:cgrectmake (85, 50, 120, 30)];    Welcomelabel.text = Appdelegate.dname;            [Self.view Addsubview:welcomelabel];    uilabel* Pwdlabel = [[UILabel alloc]initwithframe:cgrectmake (120, 100, 120, 30)];    Pwdlabel.text = Appdelegate.dpass;    [Self.view Addsubview:pwdlabel]; } @end

As follows:





Use Sharedapplication for value 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.