IOS中的全局变量和JAVA中的全局变量定义和使用方法不一样,在Java中,只需要将变量定义为static就行了。而在IOS中这种方法不适合。
IOS中定义全局变量有三种方法:
1.使用extern关键字
在AppDelegate.m或AppDelegate.h中写入你需要的全局变量名,例如:int name;注意定义全局变量时候不能初始化,否则报错。
01 | #import <UIKit/UIKit.h> |
02 | |
03 | @ class ViewController; |
04 | |
05 | int name ; |
06 | @interface AppDelegate : UIResponder <UIApplicationDelegate> |
07 | |
08 | @property (strong, nonatomic) UIWindow *window; |
09 | |
10 | @property (strong, nonatomic) ViewController *viewController; |
11 | @property (nonatomic) int y; |
12 | |
13 | @end |
在需要调用的.m文件中声明 extern int name; 然后就可以使用了。
01 | extern int name; |
02 | |
03 | @interface ViewController () |
04 | |
05 | @end |
06 | |
07 | @implementation ViewController |
08 | |
09 | - ( void )viewDidLoad |
10 | { |
11 | [super viewDidLoad]; |
12 | NSLog(@ "在另一个页面调用第一次使用之后:%d" ,name); |
13 | name = 2; |
14 | NSLog(@ "第二复制之后%d" ,name); |
15 | |
16 | AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; |
17 | NSLog(@ "appDelegate.y = %d" ,appDelegate.y); |
18 | appDelegate.y = 5; |
19 | NSLog(@ "appDelegate.y = %d" ,appDelegate.y); |
20 | |
21 | NSLog(@ "单例第一次使用:%@" ,[Singleton sharedSingleton].user); |
22 | [Singleton sharedSingleton].user = @ "单例第er次使用" ; |
23 | NSLog(@ "单例第二次使用:%@" ,[Singleton sharedSingleton].user); |
24 | // Do any additional setup after loading the view, typically from a nib. |
25 | } |
xxxAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; appDelegate.Your Variable
3.将全局变量定义未一个单例类的变量。
上说三种方式的参考代码:
01 | // |
02 | // AppDelegate.h |
03 | // _1217_GlobalDemo |
04 | // |
05 | // Created by yier on 12-12-17. |
06 | // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. |
07 | // |
08 | |
09 | #import <UIKit/UIKit.h> |
10 | |
11 | @ class ViewController; |
12 | |
13 | int name ; |
14 | @interface AppDelegate : UIResponder <UIApplicationDelegate> |
15 | |
16 | @property (strong, nonatomic) UIWindow *window; |
17 | |
18 | @property (strong, nonatomic) ViewController *viewController; |
19 | @property (nonatomic) int y; |
20 | |
21 | @end |
01 | // |
02 | // AppDelegate.m |
03 | // _1217_GlobalDemo |
04 | // |
05 | // Created by yier on 12-12-17. |
06 | // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. |
07 | // |
08 | |
09 | #import "AppDelegate.h" |
10 | |
11 | #import "ViewController.h" |
12 | #import "Singleton.h" |
13 | |
14 | extern int name; |
15 | |
16 | @implementation AppDelegate |
17 | |
18 | @synthesize window = _window; |
19 | @synthesize viewController = _viewController; |
20 | @synthesize y; |
21 | |
22 | - ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
23 | { |
24 | self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; |
25 | // Override point for customization after application launch. |
26 | self.viewController = [[ViewController alloc] initWithNibName:@ "ViewController" bundle:nil]; |
27 | NSLog(@ "未使用全局变量之前:%d" ,name); |
28 | name = 1; |
29 | NSLog(@ "第一次使用之后:%d" ,name); |
30 | self.y = 2; |
31 | [Singleton sharedSingleton].user = @ "单例第一次使用" ; |
32 | self.window.rootViewController = self.viewController; |
33 | [self.window makeKeyAndVisible]; |
34 | |
35 | |
36 | return YES; |
37 | } |
38 | |
39 | - ( void )applicationWillResignActive:(UIApplication *)application |
40 | { |
41 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. |
42 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. |
43 | } |
44 | |
45 | - ( void )applicationDidEnterBackground:(UIApplication *)application |
46 | { |
47 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. |
48 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. |
49 | } |
50 | |
51 | - ( void )applicationWillEnterForeground:(UIApplication *)application |
52 | { |
53 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. |
54 | } |
55 | |
56 | - ( void )applicationDidBecomeActive:(UIApplication *)application |
57 | { |
58 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. |
59 | } |
60 | |
61 | - ( void )applicationWillTerminate:(UIApplication *)application |
62 | { |
63 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. |
64 | } |
65 | |
66 | @end |
1 | #import <UIKit/UIKit.h> |
2 | |
3 | @interface ViewController : UIViewController |
4 | |
5 | @end |
01 | // |
02 | // ViewController.m |
03 | // _1217_GlobalDemo |
04 | // |
05 | // Created by yier on 12-12-17. |
06 | // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. |
07 | // |
08 | |
09 | #import "ViewController.h" |
10 | #import "AppDelegate.h" |
11 | #import "Singleton.h" |
12 | |
13 | |
14 | extern int name; |
15 | |
16 | @interface ViewController () |
17 | |
18 | @end |
19 | |
20 | @implementation ViewController |
21 | |
22 | - ( void )viewDidLoad |
23 | { |
24 | [super viewDidLoad]; |
25 | NSLog(@ "在另一个页面调用第一次使用之后:%d" ,name); |
26 | name = 2; |
27 | NSLog(@ "第二复制之后%d" ,name); |
28 | |
29 | AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; |
30 | NSLog(@ "appDelegate.y = %d" ,appDelegate.y); |
31 | appDelegate.y = 5; |
32 | NSLog(@ "appDelegate.y = %d" ,appDelegate.y); |
33 | |
34 | NSLog(@ "单例第一次使用:%@" ,[Singleton sharedSingleton].user); |
35 | [Singleton sharedSingleton].user = @ "单例第er次使用" ; |
36 | NSLog(@ "单例第二次使用:%@" ,[Singleton sharedSingleton].user); |
37 | // Do any additional setup after loading the view, typically from a nib. |
38 | } |
39 | |
40 | - ( void )viewDidUnload |
41 | { |
42 | [super viewDidUnload]; |
43 | // Release any retained subviews of the main view. |
44 | } |
45 | |
46 | - ( BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
47 | { |
48 | return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); |
49 | } |
50 | |
51 | @end |
01 | // Singleton.h |
02 | // _1217_GlobalDemo |
03 | // |
04 | // Created by yier on 12-12-17. |
05 | // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. |
06 | // |
07 | |
08 | #import <Foundation/Foundation.h> |
09 | |
10 | @interface Singleton : NSObject |
11 | |
12 | @property (nonatomic, retain) NSString *user; |
13 | |
14 | + (Singleton *)sharedSingleton; |
15 | |
16 | @end |
01 | // |
02 | // Singleton.m |
03 | // _1217_GlobalDemo |
04 | // |
05 | // Created by yier on 12-12-17. |
06 | // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. |
07 | // |
08 | |
09 | #import "Singleton.h" |
10 | |
11 | @implementation Singleton |
12 | @synthesize user; |
13 | |
14 | + (Singleton *)sharedSingleton{ |
15 | static Singleton *sharedSingleton = nil; |
16 | @synchronized(self){ |
17 | if (!sharedSingleton) { |
18 | sharedSingleton = [[Singleton alloc] init]; |
19 | return sharedSingleton; |
20 | } |
21 | } |
22 | return sharedSingleton; |
23 | } |
24 | @end |