如何将数组值从一个类传递给另一个类

时间:2012-03-26 05:46:30

标签: iphone

如何从委托类传递数组值以查看控制器类

const char *query2=[tempQuery UTF8String];
NSMutableArray *arr1 = [[NSMutableArray alloc]init];;
if (sqlite3_prepare_v2(database,query2,-1,&statement1,NULL)==SQLITE_OK) {
    //NSLog(@"vt", vt);

    while (sqlite3_step(statement1)==SQLITE_ROW) {
        vt=[[[Question1 alloc]init]autorelease];
        vt.question=[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement1,3)];
        [arr1 addObject:vt.question];
        NSLog(@"arr1 is %@",[arr1 description]);

其中arr1是数组值。所以数组值必须传入另一个类。

提前致谢。

4 个答案:

答案 0 :(得分:0)

在要传递此“arr1”数组的类中,执行以下步骤: 1)在那里定义一个数组(假设它是aryTest) 2)为它做财产 3)合成它

现在,您将获得此“arr1”数组。 从这个视图开始编写代码以推送到下一个视图时,请执行以下步骤:

NextViewController *objVc = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
objVc.aryTest = arr1;
[self.navigationController pushViewController:vc animated:YES];
[vc release];

这样你就可以将这个arr1数组放到aryTest数组的下一个视图中。

答案 1 :(得分:0)

您可以使用NSString方法传递NSMutableArrayNSUserDefault ... NSUserDefaultExampleSaving/Retrieving Data Using NSUserDefaults

保存

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

 // saving an NSInteger
 [prefs setInteger:42 forKey:@"integerKey"];

 // saving a Double
 [prefs setDouble:3.1415 forKey:@"doubleKey"];

 // saving a Float
 [prefs setFloat:1.2345678 forKey:@"floatKey"];

  // saving an array
  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
  [prefs setObject:data forKey:@"lastResults"]

 // This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
 [prefs synchronize];

用于检索

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];

// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];

// getting an array
 NSData *dataRepresentingSavedArray = [prefs objectForKey:@"lastResults"];
 if (dataRepresentingSavedArray != nil)
 {
    NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
    if (oldSavedArray != nil)
            objectArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
    else
            objectArray = [[NSMutableArray alloc] init];
 }

filename *detailViewController = [[filename alloc] initWithNibName:@"filename" bundle:nil];
detailViewController.audio=@"yourData";
[self presentModalViewController:detailViewController animated:YES];
[detailViewController release];

在filename.h中声明

 NSArray *audio;

 @property(nonatomic,retain) NSArray *audio;

和filename.m

 @synthesize audio;

这就是全部。

答案 2 :(得分:0)

只需在委托类中 +(NSMutableArray *)getData 创建一个数组方法

+(NSMutableArray *)getData
{
const char *query2=[tempQuery UTF8String];
NSMutableArray *arr1 = [[NSMutableArray alloc]init];;
if (sqlite3_prepare_v2(database,query2,-1,&statement1,NULL)==SQLITE_OK) {
    //NSLog(@"vt", vt);

    while (sqlite3_step(statement1)==SQLITE_ROW) {

        [arr1 addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement1,3)]];
        NSLog(@"arr1 is %@",[arr1 description]);
}
}
return [arr1 autorelease];
}
视图控制器中的

只需调用类似

的方法
 NSMutableArray * data=[[delegate getData]retain];

答案 3 :(得分:0)

尝试在appDelegate中创建数组。因此,可以通过调用appDelegate:

在任何视图控制器中访问它
myAppDelegate *myDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
arrayInView = [myDelegate msgFilter];