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