简体   繁体   中英

Calling App delegate array in ViewController

I am loading some of the arrays while I start up the application in AppDelegate. Can anyone tell me how I can call the arrays in ViewController for them to be used? The code in AppDelegate is as follows:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    Myclass *myclass = [[Myclass alloc] init];

    NSArray *W1 = [NSArray arrayWithObjects:[NSArray arrayWithObjects:[NSArray arrayWithObjects:[NSArray array], nil], nil], nil];
    //    NSArray *stringW1 = [myclass arrayFromContentsOfFileWithName:@"W1_custom2Float"];
    W1 = [myclass textToArray4dimFloat:@"W1_custom3_MOCVN_v3_wsqrt" dimension1:32 dimension2:1 dimension3:5 dimension4:5];
    //        W1 = [myclass textToArray4dimFloat:@"W1" dimension1:32 dimension2:1 dimension3:5 dimension4:5];

    //    NSLog(@"W1 is read");

    NSArray *W2 = [NSArray arrayWithObjects:[NSArray arrayWithObjects:[NSArray arrayWithObjects:[NSArray array], nil], nil], nil];
    //    NSArray *stringW2 = [myclass arrayFromContentsOfFileWithName:@"W2_custom2Float"];
    W2 = [myclass textToArray4dimFloat:@"W2_custom3_MOCVN_v3_wsqrt" dimension1:64 dimension2:32 dimension3:5 dimension4:5];
    //        W2 = [myclass textToArray4dimFloat:@"W2" dimension1:64 dimension2:32 dimension3:5 dimension4:5];

    //    NSLog(@"W2 is read");

    NSArray *Wf1 = [NSArray arrayWithObjects:[NSArray array], nil];
    //    NSArray *stringWf1 = [myclass arrayFromContentsOfFileWithName:@"Wf1_custom2Float"];
    Wf1 = [myclass textToArray2dimFloat:@"Wf1_custom3_MOCVN_v3_wsqrt" dimension1:3136 dimension2:1024];
    //        Wf1 = [myclass textToArray2dimFloat:@"Wf1" dimension1:3136 dimension2:1024];

    //    NSLog(@"Wf1 is read");

    NSArray *Wf2 = [NSArray arrayWithObjects:[NSArray array], nil];
    //    NSArray *stringWf2 = [myclass arrayFromContentsOfFileWithName:@"Wf2_custom2Float"];
    Wf2 = [myclass textToArray2dimFloat:@"Wf2_custom3_MOCVN_v3_wsqrt" dimension1:1024 dimension2:17];
    //        Wf2 = [myclass textToArray2dimFloat:@"Wf2" dimension1:1024 dimension2:14];

    //    NSLog(@"Wf2 is read");
    //
    NSArray *B1 = [NSArray array];
    //    NSArray *stringB1 = [myclass arrayFromContentsOfFileWithName:@"B1_custom2Float"];
    B1 = [myclass textToArray1dimFloat:@"B1_custom3_MOCVN_v3_wsqrt" dimensions:32];
    //    B1 = [myclass textToArray1dimFloat:@"B1" dimensions:32];

    //    NSLog(@"B1 is read");

    NSArray *B2 = [NSArray array];
    //    NSArray *stringB2 = [myclass arrayFromContentsOfFileWithName:@"B2_custom2Float"];
    B2 = [myclass textToArray1dimFloat:@"B2_custom3_MOCVN_v3_wsqrt" dimensions:64];
    //    B2 = [myclass textToArray1dimFloat:@"B2" dimensions:64];

    //    NSLog(@"B2 is read");

    NSArray *Bf1 = [NSArray array];
    //    NSArray *stringBf1 = [myclass arrayFromContentsOfFileWithName:@"Bf1_custom2Float"];
    Bf1 = [myclass textToArray1dimFloat:@"Bf1_custom3_MOCVN_v3_wsqrt" dimensions:1024];
    //    Bf1 = [myclass textToArray1dimFloat:@"Bf1" dimensions:1024];

    //    NSLog(@"Bf1 is read");

    NSArray *Bf2 = [NSArray array];
    //    NSArray *stringBf2 = [myclass arrayFromContentsOfFileWithName:@"Bf2_custom2Float"];
    Bf2 = [myclass textToArray1dimFloat:@"Bf2_custom3_MOCVN_v3_wsqrt" dimensions:17];
    //    Bf2 = [myclass textToArray1dimFloat:@"Bf2" dimensions:14];

    return YES;
}

W1, W2, Wf1, Wf2, B1, B2, Bf1, Bf2 are the arrays.

You need to add those objects into .h file

@property (strong, nonatomic)NSArray *W1;

then assign values into didFinishLaunchingWithOptions .Sample below

self.W1 = [myclass textToArray4dimFloat:@"W1_custom3_MOCVN_v3_wsqrt" dimension1:32 dimension2:1 dimension3:5 dimension4:5];

Now you can access all those property into application any where.

    #include "MyAppDelegate.h"
   NSArray *arObj = ((MyAppDelegate *)[UIApplication sharedApplication].delegate). W1;

1.declare your arrays in AppDelegate InterFace like it

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property NSArray *Bf2;

2. initialize like this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  Myclass *myclass = [[Myclass alloc] init];
  Bf2 = [NSArray array];
  Bf2 = [myclass textToArray1dimFloat:@"Bf2_custom3_MOCVN_v3_wsqrt" dimensions:17];
}

3.use in other ViewController with this

#import "AppDelegate.h"
//-------
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.Bf2.....

In AppDelegate.h

define the macro for [[UIApplication sharedApplication] delegate] in Appdelegate.h. use macro, instead of calling the method everywhere

#define appDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSArray *W1;

@end

In AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    Myclass *myclass = [[Myclass alloc] init];

    self.W1 = [myclass textToArray4dimFloat:@"W1_custom3_MOCVN_v3_wsqrt" dimension1:32 dimension2:1 dimension3:5 dimension4:5];

    return YES;
}

In ViewController.h

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

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray *array = appDelegate.W1;

    NSLog(@"%@",array);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM