2016-07-14 5 views
-3

私のAppDelegateからNSArrayを取得することはできますか?他のクラスに配列を送る必要があります。その配列は私のAppDelegateで生成されます。ありがとう!AppDelegateからNSArrayを取得できますか?

+0

はい、可能です。あなたは財産が何であるか知っていますか?あなたは '[UIApplicaton sharedApplication] .delegate'を使用しましたか? – Avi

+0

はい。 ITは可能です –

+0

まだ使用されていません – drbj

答えて

1

ファーストを支援ファイル

@property(nonatomic,retain)NSArray *books; 

AppDelegate.mで配列にオブジェクトを追加できます。 .hファイルで

_books = [[NSArray alloc]initWithObjects:@"test",@"test", nil]; 

あなたはこのようなあなたのBooksDetailViewControllerにAppDelegateインスタンスを作成する必要があります、

AppDelegate *appDelegate; 

とすることができます

appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 

今.mファイルで

このように配列にアクセスする

NSLog(@"Test Log :%@",appDelegate.books); 

出力:

2016-07-14 13:04:08.211 Gridz[2490:39240] Test Log :(
test, 
test 
) 
+0

AppDelegateにNSArray *の書籍があるとします。なぜ私はappDelegate.booksでアクセスできないのですか?私は何かを見逃しています、公共の場で本を置くか、何か? – drbj

+0

あなたはappdelegateファイルでプロパティを設定しましたか –

0
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

今、あなたのような任意のプロパティやメソッドを使用することができます。

[appDelegate myProperty]または[appDelegate myMethod]

希望これはAppDelegate.hでallocinit にあなたが持つべきすべての

+0

AppDelegateにNSArray *の書籍があるとします。どうして私は[appDelegate books]でアクセスできないのですか?書籍を公開するなど、何かが不足していますか? – drbj

+0

クラスがAppDelegate.hファイルのプロパティとして宣言されていることを確認して、クラスの外部にアクセスしてください。 – Mrunal

0

それは他のクラスに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を使用すると、私はあなたがstroyボードを使用method.If以下に、ルートビューコントローラを設定し、追加するのに十分ですNSArrayオブジェクトのみ。ルートビューコントローラを設定する必要はありません。

//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

はまたのNSLog結果である

- (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); 
} 

viewDidLoadメソッドで今すぐ

#import "ViewController.h" 
#import "AppDelegate.h" 

@interface ViewController() 

@end 

@implementation ViewController 

ViewController.m

にAppDelegateをインポートすることができます

The app delegate NSArray Objects are - (
Steve, 
jobs, 
Tim, 
Cook 
) 
関連する問題