我使用UITabBar显示两个viewControllers,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。但我尝试推动第一次竞争,它的工作原理。实际上我不想推动那个视图控制器因为我已经存在于选项卡上。
答案 0 :(得分: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;
}
}