IOS中的全局变量和JAVA中的全局变量定义和使用方法不一样,在Java中,只需要将变量定义为static就行了。而在IOS中这种方法不适合。
IOS中定义全局变量有三种方法:
1.使用extern关键字
在AppDelegate.m或AppDelegate.h中写入你需要的全局变量名,例如:int name;注意定义全局变量时候不能初始化,否则报错。
#import <UIKit/UIKit.h>
@class ViewController;
int name ;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (nonatomic) int y;
@end
在需要调用的.m文件中声明 extern int name; 然后就可以使用了。
extern int name;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"在另一个页面调用第一次使用之后:%d",name);
name = 2;
NSLog(@"第二复制之后%d",name);
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSLog(@"appDelegate.y = %d",appDelegate.y);
appDelegate.y = 5;
NSLog(@"appDelegate.y = %d",appDelegate.y);
NSLog(@"单例第一次使用:%@",[Singleton sharedSingleton].user);
[Singleton sharedSingleton].user = @"单例第er次使用";
NSLog(@"单例第二次使用:%@",[Singleton sharedSingleton].user);
// Do any additional setup after loading the view, typically from a nib.
}
2.在AppDelegate中生命并出事后全局变量,然后在需要使用该变量的地方调用如下代码
xxxAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; appDelegate.Your Variable
3.将全局变量定义未一个单例类的变量。
上说三种方式的参考代码:
//
// AppDelegate.h
// _1217_GlobalDemo
//
// Created by yier on 12-12-17.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@class ViewController;
int name ;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (nonatomic) int y;
@end
//
// AppDelegate.m
// _1217_GlobalDemo
//
// Created by yier on 12-12-17.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
#import "Singleton.h"
extern int name;
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize y;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
NSLog(@"未使用全局变量之前:%d",name);
name = 1;
NSLog(@"第一次使用之后:%d",name);
self.y = 2;
[Singleton sharedSingleton].user = @"单例第一次使用";
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// 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.
// 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.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// 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.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// 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.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// 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.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// _1217_GlobalDemo
//
// Created by yier on 12-12-17.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
#import "AppDelegate.h"
#import "Singleton.h"
extern int name;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"在另一个页面调用第一次使用之后:%d",name);
name = 2;
NSLog(@"第二复制之后%d",name);
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSLog(@"appDelegate.y = %d",appDelegate.y);
appDelegate.y = 5;
NSLog(@"appDelegate.y = %d",appDelegate.y);
NSLog(@"单例第一次使用:%@",[Singleton sharedSingleton].user);
[Singleton sharedSingleton].user = @"单例第er次使用";
NSLog(@"单例第二次使用:%@",[Singleton sharedSingleton].user);
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
// Singleton.h
// _1217_GlobalDemo
//
// Created by yier on 12-12-17.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Singleton : NSObject
@property (nonatomic, retain) NSString *user;
+ (Singleton *)sharedSingleton;
@end
//
// Singleton.m
// _1217_GlobalDemo
//
// Created by yier on 12-12-17.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "Singleton.h"
@implementation Singleton
@synthesize user;
+ (Singleton *)sharedSingleton{
static Singleton *sharedSingleton = nil;
@synchronized(self){
if (!sharedSingleton) {
sharedSingleton = [[Singleton alloc] init];
return sharedSingleton;
}
}
return sharedSingleton;
}
@end