Search code examples
nullnsarrayviewwillappear

NSArray from one view controller become NULL when using at viewWillApper method of another view controller?


i have a problem while calling NSArray from another view controller, solution may be simple but as a beginner i am confused.

here is my problem,

i am tryiin to send an array from my firstViewController to SecondViewController and i need to update the array value in to my UILabel which is on SecondViewController.

secondViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  andStationName:(NSArray *)stationName;
// stationName is from firstViewController
    {

// now the stationName have the array values


   NSArray *stationNames=[[stationName mutableCopy]retain];

 //take a copy to stationNames

 }

now i need to update this value to my UILabel

-(void)viewWillAppear:(BOOL)animated
 {
//stationNameDisplay is a uilabel

 stationNameDisplay.text=[stationNames objectAtIndex:0];
  NSLog(@"station name %@",[stationNames objectAtIndex:0]);

 }

but [stationNames objectAtIndex:0] only shows null value, i don't know why?


Solution

  • Don't try to send values through - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil method or a viewDidLoad because in a tabbar the users will switch between views without unloading the view or reinitializing the view controller.

    Declare a variable in the Application appdelegate.h as follows

    @interface AppDelegate : NSObject{
     NSMutableArray *stationnamespassed;
    }
    @property (nonatomic, retain) NSMutableArray *stationnamespassed;
    @end
    

    In appdelegate.m

    @implementation AppDelegate
    @synthesize stationnamespassed;
    
     -(void) dealloc{
              [stationnamespassed release];
              [super release];
             }
    @end
    

    In firstviewcontroller.h declare this

    @interface firstviewcontroller {
      NSMutableArray *stationnamestobepassed;
    }
    @property (nonatomic, retain) NSMutableArray *stationnamestobepassed;
    @end
    

    In firstviewcontroller.m declare this

    @implementation firstviewcontroller
    @synthesize stationnamestobepassed;
    
    -(void) someaction{
       stationnamestobepassed = [NSMutableArray     
         arrayWithObjects:@"string1",@"string2",@"string3"];
        AppDelegate *appDelegate=[[UIApplication sharedApplicaton] delegate];
        appDelegate.stationnamespassed = self.stationnamestobepassed;
    }
    
    -(void) dealloc{
      [stationnamestobepassed release];
      [super release];
     }
    @end
    

    In secondviewcontroller.m access app delegate variable as follows

    @implementation secondviewcontroller
    
       -(void)viewWillAppear:(BOOL)animated
     {
    //stationNameDisplay is a uilabel
    AppDelegate *appDelegate=[[UIApplication sharedApplicaton] delegate];
     stationNameDisplay.text=[appDelegate.stationnamespassed objectAtIndex:0];
      NSLog(@"station name %@",[appDelegate.stationnamespassed objectAtIndex:0]);
    
     }
    -(void) dealloc{
          [super release];
         }
    @end
    

    Try this way.For more info on different ways of persisting data across different view controllers, Read this article.