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

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

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

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

通过Delegate来传递。

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

(1)AppDelegate.h

 

[objc] 
​​view plain​​​
​​​copy​​



1. #import <UIKit/UIKit.h>
2.
3. @interface AppDelegate : UIResponder <UIApplicationDelegate>
4.
5. @property (strong, nonatomic) UIWindow *window;
6. @property (strong, nonatomic) NSString *dname;//定义dname传递帐号的值
7. @property (strong, nonatomic) NSString *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. alloc]initWithFrame:CGRectMake(30, 50, 50, 30)];
15. .text = @"账号";
16. self.view addSubview:nameLabel];
17.
18. alloc]initWithFrame:CGRectMake(30, 100, 50, 30)];
19. .text = @"密码";
20. self.view addSubview:passLabel];
21.
22. alloc]initWithFrame:CGRectMake(100, 50, 150, 30)];
23. .borderStyle = UITextBorderStyleRoundedRect;
24. self.view addSubview:nameTextField];
25.
26. alloc]initWithFrame:CGRectMake(100, 100, 150, 30)];
27. .borderStyle = UITextBorderStyleRoundedRect;
28. self.view addSubview:passTextField];
29.
30. buttonWithType:UIButtonTypeSystem];
31. .frame = CGRectMake(60, 180, 72, 30);
32. setTitle:@"登陆" forState:UIControlStateNormal];
33. self.view addSubview:loginBtn];
34. 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. .dname = nameTextField.text; ;
43. .dpass = passTextField.text;
44. 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. alloc]initWithFrame: CGRectMake(30, 50, 150, 30)];
13. .text = @"欢迎您,";
14. self.view addSubview:helloLabel];
15.
16.
17.
18. alloc]initWithFrame: CGRectMake(30, 100, 150, 30)];
19. .text = @"您的密码是,";
20. self.view addSubview:passLabel];
21.
22. alloc]initWithFrame:CGRectMake(85, 50, 120, 30)];
23. .text = appdelegate.dname;
24. self.view addSubview: welcomeLabel];
25.
26.
27. alloc]initWithFrame:CGRectMake(120, 100, 120, 30)];
28. .text = appdelegate.dpass;
29. self.view addSubview: pwdLabel];
30.
31. }
32.
33.
34.
35. @end



 

效果图如下:

  

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

 

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