在ViewController中调用App委托数组

时间:2016-04-26 08:57:31

标签: ios objective-c nsarray viewcontroller appdelegate

我在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是阵列。

3 个答案:

答案 0 :(得分:1)

您需要将这些对象添加到.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 :(得分:1)

1.在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.使用此

在其他ViewController中使用
#import "AppDelegate.h"
//-------
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.Bf2.....

答案 2 :(得分:0)

AppDelegate.h

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

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