[IOS](转)使用SharedApplication进行传值

转自:http://blog.csdn.net/panjican/article/details/51332219 

 

一般而言,在iOS中页面间传值,常见的方法有四种,

    使用SharedApplication,定义一个变量来传递.

   2 使用文件plist,或者NSUserdefault来传递

   3 通过一个单例的class来传递

   4 通过Delegate来传递。

     我先学习一下第一种方法,下面为范例:

(1)AppDelegate.h

 

[objc]  view plain  copy
 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  4.   
  5. @property (strongnonatomicUIWindow *window;  
  6. @property (strongnonatomicNSString *dname;//定义dname传递帐号的值  
  7. @property (strongnonatomicNSString *dpass;//定义dpass传递密码的值  
  8.   
  9. @end  


    AppDelegate.m

 

 

[objc]  view plain  copy
 
  1. #import "AppDelegate.h"  
  2. #import "ViewController.h"  
  3.   
  4. @interface AppDelegate ()  
  5.   
  6. @end  
  7.   
  8. @implementation AppDelegate  
  9.   
  10.   
  11. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  12.   
  13.     //create the window  
  14.     self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];  
  15.     self.window.backgroundColor = [UIColor whiteColor];  
  16.     self.window.rootViewController = [[ViewController alloc]init];  
  17.     [self.window makeKeyAndVisible];   
  18.     return YES;  
  19. }  
  20.   
  21. @end  



 

(2)ViewController.h

 

[objc]  view plain  copy
 
  1. #import <UIKit/UIKit.h>  
  2. #import "AppDelegate.h"  
  3.   
  4. @interface ViewController : UIViewController  
  5.   
  6.   
  7. @end  


  ViewController.m

 

 

[objc]  view plain  copy
 
  1. #import "ViewController.h"  
  2. #import "NavViewController.h"  
  3.   
  4. UITextField* nameTextField;  
  5. UITextField *passTextField;  
  6. @interface ViewController ()  
  7.   
  8. @end  
  9.   
  10. @implementation ViewController  
  11.   
  12. - (void)viewDidLoad {  
  13.     [super viewDidLoad];  
  14.     UILabel* nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30505030)];  
  15.     nameLabel.text = @"账号";  
  16.     [self.view addSubview:nameLabel];  
  17.       
  18.     UILabel* passLabel = [[UILabel alloc]initWithFrame:CGRectMake(301005030)];  
  19.     passLabel.text = @"密码";  
  20.     [self.view addSubview:passLabel];  
  21.       
  22.     nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(1005015030)];  
  23.     nameTextField.borderStyle = UITextBorderStyleRoundedRect;  
  24.     [self.view addSubview:nameTextField];  
  25.       
  26.     passTextField =  [[UITextField alloc]initWithFrame:CGRectMake(10010015030)];  
  27.     passTextField.borderStyle = UITextBorderStyleRoundedRect;  
  28.     [self.view addSubview:passTextField];  
  29.       
  30.     UIButton* loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];  
  31.     loginBtn.frame = CGRectMake(601807230);  
  32.     [loginBtn setTitle:@"登陆" forState:UIControlStateNormal];  
  33.     [self.view addSubview:loginBtn];  
  34.     [loginBtn addTarget:self action:@selector(onLogin:) forControlEvents:UIControlEventTouchUpInside];  
  35.       
  36.       
  37. }  
  38.   
  39. - (void) onLogin: (id) sender  
  40. {  
  41.     AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];  
  42.     appDelegate.dname = nameTextField.text; ;  
  43.     appDelegate.dpass = passTextField.text;  
  44.         NavViewController* navCtr = [[NavViewController alloc] init];  
  45.         [self presentViewController:navCtr animated:YES completion:nil];     
  46. }  
  47.   
  48. @end  


(3) NavViewController.h

 

 

[objc]  view plain  copy
 
  1. #import <UIKit/UIKit.h>  
  2. #import "AppDelegate.h"  
  3.   
  4. @interface NavViewController : UIViewController  
  5.   
  6. @end  


  NavViewController.m

 

 

[objc]  view plain  copy
 
  1. #import "NavViewController.h"  
  2. UILabel* welcomeLabel;  
  3. @interface NavViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation NavViewController  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.     AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];  
  12.     UILabel* helloLabel = [[UILabel alloc]initWithFrame: CGRectMake(305015030)];  
  13.     helloLabel.text = @"欢迎您,";  
  14.     [self.view addSubview:helloLabel];  
  15.       
  16.       
  17.       
  18.     UILabel* passLabel = [[UILabel alloc]initWithFrame: CGRectMake(3010015030)];  
  19.     passLabel.text = @"您的密码是,";  
  20.     [self.view addSubview:passLabel];  
  21.       
  22.     UILabel* welcomeLabel = [[UILabel alloc]initWithFrame:CGRectMake(855012030)];  
  23.     welcomeLabel.text = appdelegate.dname;  
  24.     [self.view addSubview: welcomeLabel];  
  25.       
  26.       
  27.     UILabel* pwdLabel = [[UILabel alloc]initWithFrame:CGRectMake(12010012030)];  
  28.     pwdLabel.text = appdelegate.dpass;  
  29.     [self.view addSubview: pwdLabel];  
  30.       
  31. }  
  32.   
  33.   
  34.   
  35. @end  

效果图如下:

  

 

猜你喜欢

转载自jameskaron.iteye.com/blog/2394167