在 ViewController 中调用 App 委托数组

Calling App delegate array in ViewController

我在 AppDelegate 中启动应用程序时加载了一些数组。谁能告诉我如何调用 ViewController 中的数组以供使用? AppDelegate中的代码如下:

- (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 是数组。

您需要将这些对象添加到 .h文件

@property (strong, nonatomic)NSArray *W1;

然后将值赋给 didFinishLaunchingWithOptions。下面的示例

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

现在您可以在任何地方将所有这些 属性 访问到应用程序中。

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

1.declare你在 AppDelegate InterFace 中的数组喜欢它

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

2。像这样初始化

- (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 在其他 ViewController 与此

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

AppDelegate.h

在 Appdelegate.h 中为 [[UIApplication sharedApplication] delegate] 定义宏。使用宏,而不是到处调用方法

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>

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

@end

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

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