Search code examples
iphoneuitabbarcontrollerupdating

UI is not updating while sending data from another view controller on UITabBar?


i am using UITabBar to display two viewControllers, firstViewController is a uiview and secondViewController is a table view. what my need is when i clicked the second view table cell, the value should be updated on UILabel of firstViewController. my code is here, In secondviewcontroller.h

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

In secondviewcontroller.m declare this

@implementation firstviewcontroller
@synthesize stationnamestobepassed;

 -(void) someaction{
  stationnamestobepassed = [NSMutableArray     
    arrayWithObjects:@"string1",@"string2",@"string3"];
  secondviewcontroller = [[secondviewcontroller alloc]initWithNibName:@"NIbName" 
Bundle:nil];
  secondviewcontroller.stationnamespassed = self.stationnamestobepassed;
  //i am not pushing the view controller.

 }

@end

In firstviewcontroller.h declare this

@interface secondviewcontroller {
 NSMutableArray *stationnamespassed;
}
@property (nonatomic, retain) NSMutableArray *stationnamespassed;


@end

In firstviewcontroller.m declare this

@implementation secondviewcontroller
@synthesize stationnamespassed;

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

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

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

problem is , value is not updating and it gives NULL. but i tried with pushing the first vie and it works. actually i don't want to push that view controller because i was already exist on tab.


Solution

  • your view will appear called out when you select the tab. while this line makes a new object of that controller, not that of tab controller.

    secondviewcontroller = [[secondviewcontroller alloc]initWithNibName:@"NIbName" 
    Bundle:nil];
    

    to do what you want. you will have to do some thing like this

    UINavigationController* navController = [[self.tabBarController viewControllers] objectAtIndex:/*your tab index on which desiredControllerResides*/];
    NSArray* viewControllers= [navController viewControllers];
    for(UIViewController *theController in viewControllers)
    {
    if([theController isKindOfClass:[secondviewcontroller]])
    {
       theController.stationnamespassed = self.stationnamestobepassed;
    //i.e  this line of your code secondviewcontroller.stationnamespassed = self.stationnamestobepassed;
    }
    }