是否可以从 AppDelegate 获取 NSArray?
Is it possible to get a NSArray from AppDelegate?
是否可以从我的 AppDelegate 获取 NSArray?我需要向其他 类 发送一个数组。该数组是在我的 AppDelegate 中生成的。谢谢!
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
现在您可以使用任何 属性 或以下方法:
[appDelegate myProperty]
或 [appDelegate myMethod]
希望对您有所帮助
首先你必须 alloc
和 init
在 AppDelegate.h 文件中
@property(nonatomic,retain)NSArray *books;
在 AppDelegate.m 中归档您的数组,然后您可以在其中添加对象。
_books = [[NSArray alloc]initWithObjects:@"test",@"test", nil];
您应该像这样在 BooksDetailViewController
中创建 AppDelegate 实例,
在 .h 文件中
AppDelegate *appDelegate;
在 .m 文件中
appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
现在您可以像这样访问数组了,
NSLog(@"Test Log :%@",appDelegate.books);
输出:
2016-07-14 13:04:08.211 Gridz[2490:39240] Test Log :(
test,
test
)
可以从 AppDelegate 获取 NSArray 到其他 类。
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
}
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) ViewController *viewController; //For Xib
@property (nonatomic, strong) NSArray *arrayObjects;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
@synthesize arrayObjects;
因为我用的是Xib,我在下面设置了root view controller method.If你用stroy board,添加NSArray对象就可以了only.Don不需要设置root view controller。
//For XIB
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
arrayObjects = [[NSArray alloc]initWithObjects:@"Steve",@"jobs",@"Tim",@"Cook",nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[navController setNavigationBarHidden:YES];
[self.window makeKeyAndVisible];
return YES;
}
//For Storyboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
arrayObjects = [[NSArray alloc]initWithObjects:@"Steve",@"jobs",@"Tim",@"Cook",nil];
return YES;
}
然后在ViewController.m
您还可以在 ViewController.m
中导入 AppDelegate
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@end
@implementation ViewController
现在在 viewDidLoad 方法中
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSLog(@"The app delegate NSArray Objects are - %@",delegate.arrayObjects);
}
NSLog 结果是
The app delegate NSArray Objects are - (
Steve,
jobs,
Tim,
Cook
)
是否可以从我的 AppDelegate 获取 NSArray?我需要向其他 类 发送一个数组。该数组是在我的 AppDelegate 中生成的。谢谢!
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
现在您可以使用任何 属性 或以下方法:
[appDelegate myProperty]
或 [appDelegate myMethod]
希望对您有所帮助
首先你必须 alloc
和 init
在 AppDelegate.h 文件中
@property(nonatomic,retain)NSArray *books;
在 AppDelegate.m 中归档您的数组,然后您可以在其中添加对象。
_books = [[NSArray alloc]initWithObjects:@"test",@"test", nil];
您应该像这样在 BooksDetailViewController
中创建 AppDelegate 实例,
在 .h 文件中
AppDelegate *appDelegate;
在 .m 文件中
appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
现在您可以像这样访问数组了,
NSLog(@"Test Log :%@",appDelegate.books);
输出:
2016-07-14 13:04:08.211 Gridz[2490:39240] Test Log :(
test,
test
)
可以从 AppDelegate 获取 NSArray 到其他 类。
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate> {
}
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) ViewController *viewController; //For Xib
@property (nonatomic, strong) NSArray *arrayObjects;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
@synthesize arrayObjects;
因为我用的是Xib,我在下面设置了root view controller method.If你用stroy board,添加NSArray对象就可以了only.Don不需要设置root view controller。
//For XIB
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
arrayObjects = [[NSArray alloc]initWithObjects:@"Steve",@"jobs",@"Tim",@"Cook",nil];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[navController setNavigationBarHidden:YES];
[self.window makeKeyAndVisible];
return YES;
}
//For Storyboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
arrayObjects = [[NSArray alloc]initWithObjects:@"Steve",@"jobs",@"Tim",@"Cook",nil];
return YES;
}
然后在ViewController.m
您还可以在 ViewController.m
中导入 AppDelegate#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@end
@implementation ViewController
现在在 viewDidLoad 方法中
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSLog(@"The app delegate NSArray Objects are - %@",delegate.arrayObjects);
}
NSLog 结果是
The app delegate NSArray Objects are - (
Steve,
jobs,
Tim,
Cook
)