0

我正在使用 UITabBar 来显示两个 viewController,firstViewController 是一个 uiview,secondViewController 是一个表格视图。我需要的是当我单击第二个视图表单元格时,该值应该在 firstViewController 的 UILabel 上更新。我的代码在这里,在secondviewcontroller.h

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

secondviewcontroller.m声明这个

@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

firstviewcontroller.h声明这个

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


@end

firstviewcontroller.m声明这个

@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

问题是,值没有更新,它给出了 NULL。但我尝试推动第一个 vie 并且它有效。实际上我不想推送那个视图控制器,因为我已经存在于选项卡上。

4

1 回答 1

1

当您选择选项卡时,您的视图将被调出。而这一行创建了该控制器的新对象,而不是选项卡控制器的新对象。

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

做你想做的事。你将不得不做这样的事情

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;
}
}
于 2012-05-21T05:03:21.687 回答