初始化后在视图控制器中加载/刷新新数据
问题描述:
我有一个建筑类,其中包含建筑信息(字符串)。当我推动我的DetailViewController(不是从一个tableviewcontroller),我推它的建筑selcyed它加载在正确的信息。我想要做的是在下一个建筑物实例中加载的详细视图上有一个按钮。我尝试了以下,但没有运气 - 所有数据都为空/空。我不能正确地连接到我的建筑物。初始化后在视图控制器中加载/刷新新数据
在我mainViewController
-(IBAction)pushDetailsVC:(id)sender {
DetailViewController* controller = [[DetailViewController alloc] init];
// Pass the Building object to the controller
building = [self.buildings objectAtIndex:nsu_selBldg];
controller.building = building;
NSLog(@"Building name is now: %@", building);
[self presentModalViewController:controller animated:YES];
[controller.uib_backButton addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
[controller release];
}
在我DetailViewController // h的
@property (nonatomic, retain) Building *building;
@property (nonatomic, retain) PhotoViewerViewController *photoVC;
// m的
- (void)viewDidLoad
{
[super viewDidLoad];
// This works as expected - the data loads in great.
self.uill_bldgName.text = self.building.name;
self.uitt_bldgInfo.text = self.building.bldgInfo;
self.uiivv_bldgPlan.image = [UIImage imageNamed:self.building.bldgPlan];
self.uiivv_bldgImage.image = [UIImage imageNamed:self.building.bldgImageZoom];
NSLog(@"first name%@",self.building.bldgImageZoom);
}
-(IBAction)nextBldg {
// This does not work - data goes null. PhotoVC is my main view controller and buildings is the array of buildings
building = [photoVC.buildings objectAtIndex:2];
Building *building = building;
NSLog(@"building %@",building);
NSLog(@"building %@",[photoVC.buildings objectAtIndex:2]);
self.uill_bldgName.text = self.building.name;
self.uitt_bldgInfo.text = self.building.bldgInfo;
self.uiivv_bldgPlan.image = [UIImage imageNamed:self.building.bldgPlan];
self.uiivv_bldgImage.image = [UIImage imageNamed:self.building.bldgImageZoom];
NSLog(@"secon name %@",self.building.bldgImageZoom);
}
答
我没有看到你在哪里初始化photoVC。没有初始化,你一定没有建筑数据。
更好的方法是将您的建筑物数组(和当前索引nsu_selBldg)传递给DetailViewController。然后它将可以访问任何想要的建筑物。
// DetailVC.h
@property (nonatomic, retain) NSArray *buildings;
@property (nonatomic, assign) NSUInteger currentIndex;
// DetailVC.m
Building *currentBuilding = [self.buildings objectAtIndex:self.currentIndex];
NSUInteger nextIndex = (self.currentIndex == self.buildings.count-1)? 0 : self.currentIndex+1;
Building *nextBuilding = [self.buildings objectAtIndex:nextIndex];
工作很好!我将self.currentIndex + 1更改为self.currentIndex ++。 – malaki1974 2012-04-05 12:53:26