I have a Building class that holds building info (strings). When I push my DetailViewController (not from a tableviewcontroller) I push the building selcyed with it and it loads in the right info. What I want to do is have a button on the detail view that loads in the next instance of building. I tried the following but no luck - all the data goes blank/null. I must not be connecting to my array of buildings properly.
In my 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];
}
In my DetailViewController // h has
@property (nonatomic, retain) Building *building;
@property (nonatomic, retain) PhotoViewerViewController *photoVC;
//m has
- (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);
}
I don't see where you initialize photoVC. Without initializing, you'll surely have no building data.
The better approach is to pass your building array (and the current index nsu_selBldg) to the DetailViewController. Then it will have access to any building it wants.
// 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];