2

How can i pass an array value from a delegate class to view controller class

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

Where arr1 is an array value. So that array value has to pass in another class.

Thanks in advance.

user801222
  • 49
  • 2

4 Answers4

0

In a class where you want to pass this "arr1" array do following steps: 1) define a array there (lets say it is aryTest) 2) make property for it 3) synthesize it

Now in a view where you are getting this "arr1" array. From this view when ever you write code to push to next view do following steps:

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

so by this you will get this arr1 array into next view in aryTest array.

Prashant Bhayani
  • 692
  • 5
  • 18
0

You can pass NSString or NSMutableArray using NSUserDefault method... NSUserDefaultExample and Saving/Retrieving Data Using NSUserDefaults

For Saving

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

For Retrieving

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

or

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

Declare in filename.h

 NSArray *audio;

 @property(nonatomic,retain) NSArray *audio;

and filename.m

 @synthesize audio;

thats all.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
0

just create a Array method like in +(NSMutableArray *)getData in delegate class

+(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];
}

in your view controller just call the method like

 NSMutableArray * data=[[delegate getData]retain];
Rams
  • 1,721
  • 12
  • 22
0

Try creating the array in your appDelegate. Thus it can be accessible in any view controller by calling your appDelegate:

myAppDelegate *myDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
arrayInView = [myDelegate msgFilter];
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
sansid1983
  • 239
  • 1
  • 4
  • 14