开发者

how pass array value on other class for display data in iphone

Hi friends thank for helping to me I have doubt when i pass one array value for display on other controller class in tableView so I get nil value on that controller how to take array value on that class of other controller for display purpose

my lstAirports is a array which created on Airport.h and my Airport.h is simple class is not delegate the code of this class:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{

    loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    [connection release];
    NSString *regexString = @"Stations\\[""(.*)""\\] = new Station\\((.*)new Array\\((.*)\\)\\);";
    matchArray = [loginStatus arrayOfCaptureComponentsMatchedByRegex:regexString];
    //NSLog(@"matchArray: %@", matchArray);
    lstAirports = [[NSMutableArray alloc] initWithCapacity:[matchArray count]];

    for (int i = 0; i < [matchArray count]; i++) {
        airport *air=[[airport alloc]init];

        //code
        NSString *temp=[[matchArray objectAtIndex: i] objectAtIndex: 1];
        NSString *newString=[temp stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        arrParts=[newString componentsSeparatedByString:@","];
        air.Code =[arrParts objectAtIndex:0];
        //air.Code = [[matchArray objectAtIndex: i] objectAtIndex: 1];
        NSLog(@"air.Code: %@\n",air.Code);

        //name      
        temp=[[matchArray objectAtIndex: i] objectAtIndex: 2];
        newString=[temp stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        arrParts=[newString componentsSeparatedByString:@","];
        air.Name=[arrParts objectAtIndex:2];

        NSLog(@"air.Name: %@\n",air.Name);

        //destination airports
        temp=[[matchArray objectAtIndex: i] objectAtIndex: 3];
        newString=[temp stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        arrParts=[newString componentsSeparatedByString:@","];
        air.DestinationAirports =arrParts;
        NSLog(@"air.DestinationAirports: %@\n",air.DestinationAirports);
        [lstAirports addObject: air];
        NSLog(@"lstAirports: %@\n",lstAirports);
        //NSString *str=
        //[air release];
    }

}




- (void)dealloc {
    [super dealloc];
    [loginStatus release]; 
    //[lstAirports release];
    [webData release];
   // [window release];


}



When I pass this array on my `Odselectioncontroller.m` then I get nil value of array where I am wrong friends please help me out this is my controller cl开发者_Go百科ass code


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    return obj.lstAirports.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }



    airport *a=(airport*)[obj.lstAirports objectAtIndex:indexPath.row];
    NSLog(@"str:%@",a);
    cell.textLabel.text =a.Name;

and `Airport` is other class where i am create name property for fetch value name from array in yhis class


You are always creating new instance of your Airports class obj which will return you nil value always. If you want to pass a array from one to other then you can do it by two ways-

  1. make your array global by using extern keyword.

  2. you can define a array in second class as a property and then can set it.

for more you can refer this -

Passing data between classes using Objective-C


DO one thing declare the NSMutable Array in AppDelegate and synthesize the array and using this array to store objects in any class and use this array to display in any class directly by using appDelegate.ArrayName Hope it helps You :)


Here you can do like, Airport.h class as Sigleton and use . Otherwise Declare a Array in APPDelegate and access it in you needed class by sharedObject mechanism


Either you can pass the array using an array object which is synthesized in the class. Another option is to have a method(which takes a NSArray object as parameter) in the class you want to populate and call the method on your first class passing the array as parameter. Your code also requires a slight change in the dealloc method. Its advised not to call [super dealloc]; before releasing the objects of the class. Always call [super dealloc]; at the end after releasing all objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜