IOS中的全局变量和JAVA中的全局变量定义和使用方法不一样,在Java中,只需要将变量定义为static就行了。而在IOS中这种方法不适合。
IOS中定义全局变量有三种方法:
1.使用extern关键字
在AppDelegate.m或AppDelegate.h中写入你需要的全局变量名,例如:int name;注意定义全局变量时候不能初始化,否则报错。
1#import <UIKit/UIKit.h> 2 3@class ViewController; 4 5int name ; 6@interface AppDelegate : UIResponder <UIApplicationDelegate> 7 8@property (strong, nonatomic) UIWindow *window; 9 10@property (strong, nonatomic) ViewController *viewController; 11@property (nonatomic) int y; 12 13@end
在需要调用的.m文件中声明 extern int name; 然后就可以使用了。
1extern int name; 2 3@interface ViewController () 4 5@end 6 7@implementation ViewController 8 9- (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}
2.在AppDelegate中生命并出事后全局变量,然后在需要使用该变量的地方调用如下代码
xxxAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; appDelegate.Your Variable
3.将全局变量定义未一个单例类的变量。
上说三种方式的参考代码:
1// 2// AppDelegate.h 3// _1217_GlobalDemo 4// 5// Created by yier on 12-12-17. 6// Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 7// 8 9#import <UIKit/UIKit.h> 10 11@class ViewController; 12 13int 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 22 23// 24// AppDelegate.m 25// _1217_GlobalDemo 26// 27// Created by yier on 12-12-17. 28// Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 29// 30 31#import "AppDelegate.h" 32 33#import "ViewController.h" 34#import "Singleton.h" 35 36extern int name; 37 38@implementation AppDelegate 39 40@synthesize window = _window; 41@synthesize viewController = _viewController; 42@synthesize y; 43 44- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 45{ 46 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 47 // Override point for customization after application launch. 48 self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 49 NSLog(@"未使用全局变量之前:%d",name); 50 name = 1; 51 NSLog(@"第一次使用之后:%d",name); 52 self.y = 2; 53 [Singleton sharedSingleton].user = @"单例第一次使用"; 54 self.window.rootViewController = self.viewController; 55 [self.window makeKeyAndVisible]; 56 57 58 return YES; 59} 60 61- (void)applicationWillResignActive:(UIApplication *)application 62{ 63 // 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. 64 // 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. 65} 66 67- (void)applicationDidEnterBackground:(UIApplication *)application 68{ 69 // 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. 70 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 71} 72 73- (void)applicationWillEnterForeground:(UIApplication *)application 74{ 75 // 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. 76} 77 78- (void)applicationDidBecomeActive:(UIApplication *)application 79{ 80 // 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. 81} 82 83- (void)applicationWillTerminate:(UIApplication *)application 84{ 85 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 86} 87 88@end 89 90#import <UIKit/UIKit.h> 91 92@interface ViewController : UIViewController 93 94@end 95 96// 97// ViewController.m 98// _1217_GlobalDemo 99// 100// Created by yier on 12-12-17. 101// Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 102// 103 104#import "ViewController.h" 105#import "AppDelegate.h" 106#import "Singleton.h" 107 108 109extern int name; 110 111@interface ViewController () 112 113@end 114 115@implementation ViewController 116 117- (void)viewDidLoad 118{ 119 [super viewDidLoad]; 120 NSLog(@"在另一个页面调用第一次使用之后:%d",name); 121 name = 2; 122 NSLog(@"第二复制之后%d",name); 123 124 AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 125 NSLog(@"appDelegate.y = %d",appDelegate.y); 126 appDelegate.y = 5; 127 NSLog(@"appDelegate.y = %d",appDelegate.y); 128 129 NSLog(@"单例第一次使用:%@",[Singleton sharedSingleton].user); 130 [Singleton sharedSingleton].user = @"单例第er次使用"; 131 NSLog(@"单例第二次使用:%@",[Singleton sharedSingleton].user); 132 // Do any additional setup after loading the view, typically from a nib. 133} 134 135- (void)viewDidUnload 136{ 137 [super viewDidUnload]; 138 // Release any retained subviews of the main view. 139} 140 141- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 142{ 143 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 144} 145 146@end 147 148// Singleton.h 149// _1217_GlobalDemo 150// 151// Created by yier on 12-12-17. 152// Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 153// 154 155#import <Foundation/Foundation.h> 156 157@interface Singleton : NSObject 158 159@property (nonatomic, retain) NSString *user; 160 161+ (Singleton *)sharedSingleton; 162 163@end 164 165// 166// Singleton.m 167// _1217_GlobalDemo 168// 169// Created by yier on 12-12-17. 170// Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 171// 172 173#import "Singleton.h" 174 175@implementation Singleton 176@synthesize user; 177 178+ (Singleton *)sharedSingleton{ 179 static Singleton *sharedSingleton = nil; 180 @synchronized(self){ 181 if (!sharedSingleton) { 182 sharedSingleton = [[Singleton alloc] init]; 183 return sharedSingleton; 184 } 185 } 186 return sharedSingleton; 187} 188@end