0

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.

Saad
  • 8,857
  • 2
  • 41
  • 51
Neeraj Neeru
  • 570
  • 8
  • 28

1 Answers1

1

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;
}
}
Saad
  • 8,857
  • 2
  • 41
  • 51
  • i got the value updated while i put this line on secondViewController.m after initiating [self.navigationController pushViewController:firstview animated:YES];, but i really don't want to push the view controller, i just want to update it. and viewDidLoad only calls once i think. my view controllers are in tabbarcontroller? – Neeraj Neeru May 21 '12 at 05:18
  • is your view appearing? i think your view is not appearing on screen? em i right? – Saad May 21 '12 at 05:20
  • yes, sure its appearing. but only when i selected the tab for firstViewController its not updated by the value from secondViewController. – Neeraj Neeru May 21 '12 at 05:24
  • thank you for your feedback, sorry am confused, actually where do i put this code? secondviewcontroller.m? – Neeraj Neeru May 21 '12 at 05:45
  • just where you are setting the value. i've mention your code line. replace secondviewcontroller = [[secondviewcontroller alloc]initWithNibName:@"NIbName" Bundle:nil]; secondviewcontroller.stationnamespassed = self.stationnamestobepassed; with my given code – Saad May 21 '12 at 05:56
  • can you please specify this object theViewController – Neeraj Neeru May 21 '12 at 06:04
  • look. you want to set property of secondViewController which is in tabbar, but u'r making your own object with no effect ont he viewController in tabbar, so will have to first get the navigation controller on the index of tab bar where secondviewcontroller resides, then in that navigation controller, search all the viewcontrollers and check which one is the secondviewcontroller by matching class. theViewController is the required viewcontroller of whcih you want to set property – Saad May 21 '12 at 06:11
  • okay, i got some errors while adding those codes, am sure u have the right answer, sorry am just a beginner and confused sometimes about the objects, if([theViewController isKindOfClass:[secondviewcontroller]]) which objects is this? i have set the property for secondViewController – Neeraj Neeru May 21 '12 at 06:24
  • unknown receiver "theviewController" did you mean "UIViewController" and Unknown type name "theviewController" – Neeraj Neeru May 21 '12 at 06:29
  • sorry this was theController, really sorry i didn't saw that. see edited anser – Saad May 21 '12 at 06:32
  • okay let me explain what i understand from your code , secondviewcontroller is the object on secondViewController , right? and still have the errors, so what is this object theController? – Neeraj Neeru May 21 '12 at 06:40
  • i can't call theController.stationnamespassed – Neeraj Neeru May 21 '12 at 06:41