I’m working on a large project that consists of several sections, each with a varying number of slides.
I’m trying to dynamically create a navigation bar where the number of buttons correspond with the loaded section’s slides. The issue I have is setting the on/off appearance of the button as each slide loads.
I create the navigation bar when the section is created and the appearance of each button should be updated as each slide is loaded. I am trying to target each button using viewWithTag. The appearance is fine when the first slide of the section loads, but when I hit the next button everything disappears. Curiously, when clicking back to the first slide, everything appears as it should.
When a section is loaded, a mutable array of dictionary objects is created containing the necessary slide information. I have a class method (- (void) newbutton:(NSString *) title withIndex:(int) index atPosition:(int) Xpos) that is called from the Main View that creates a custom “ON” and “OFF” button through a for-loop, passing title and index arguments from a plist file. After that, the slide at index 0 is loaded.
Main View
- (void) findStory:(NSString *) presentation
{
[model loadStory:presentation];
int storyCount = model.currentStory.count;
if (storyCount > 1) {
int distribution = 700 / (storyCount - 1); // 700 = width of Navigation Rule
int Xpos = 89; // 89 = X position of Navigation Rule
// Build Navidation trail
for (int i = 0; i < model.currentStory.count; i++) {
NSLog(@"findStory(): Item Title: %@", [model.currentStory[i] objectForKey:@"name"]);
[nav newbutton:[model.currentStory[i] objectForKey:@"name"] withIndex:i atPosition:Xpos];
navButton = nav.button;
navDot = nav.buttonDot;
[self.navHolder addSubview:navDot];
[self.navHolder addSubview:navButton];
Xpos += distribution;
}
}
[self newPage:model.pageIndex];
}
- (void)newPage:(int) index
{
NSLog(@"newPage(): Requested page Index is %i", model.pageIndex);
// Code that finds and loads the slide
// At the end the navigation appearance is set
[self setNavAppearance:index];
}
- (void) setNavAppearance:(int) index
{
NSLog(@"setNavAppearance(): Setting navigation appearance...");
int storyCount = model.currentStory.count;
if (storyCount > 1) {
for (int i = 0; i < storyCount; i++) {
int pressedIndex = i + 20;
int viewTag = i;
UIButton *onButton = (UIButton *)[self.navHolder viewWithTag:viewTag];
if (i != index) {
onButton.hidden = YES;
} else {
onButton.hidden = NO;
}
UIButton *offButton = (UIButton *)[self.navHolder viewWithTag:pressedIndex];
offButton.hidden = NO;
}
}
NSLog(@"setNavAppearance(): navigation appearance set.");
}
nav Class Method
- (void) newbutton:(NSString *) title withIndex:(int) index atPosition:(int) Xpos
{
buttonIndex = index;
button = [[UIButton alloc] init];
int labelXPosition = (Xpos - 75);
int dotXposition = (Xpos - 10);
// ON Button
buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelXPosition, 637, 150, 55)];
buttonLabel.textColor = [UIColor blackColor];
buttonLabel.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:12];
buttonLabel.textAlignment = NSTextAlignmentCenter;
buttonLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonLabel.numberOfLines = 2;
buttonLabel.text = title;
buttonLabel.backgroundColor = [UIColor clearColor];
buttonLabel.textColor = [UIColor colorWithRed:0.047f green:0.337f blue:0.494f alpha:1.0f];
[self.button addSubview:buttonLabel];
dot = [[UIImageView alloc] initWithFrame:CGRectMake(dotXposition, 702, 20, 20)];
[dot setImage:[UIImage imageNamed:@"buttonPressed.png"]];
[self.button addSubview:dot];
button.tag = buttonIndex;
// OFF Button
buttonDot = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonDot.frame = CGRectMake(dotXposition, 702, 20, 20);
buttonDot.backgroundColor = [UIColor clearColor];
UIImage *dotImage = [UIImage imageNamed:@"button.png"];
[buttonDot setBackgroundImage:dotImage forState:UIControlStateNormal];
UIImage *dotImagePress = [UIImage imageNamed:@"buttonPressed"];
[buttonDot setBackgroundImage:dotImagePress forState:UIControlStateHighlighted];
int dotIndex = buttonIndex + 20;
buttonDot.tag = dotIndex;
}
I’m not sure what’s going on. The -(void) setNavAppearance method runs every time a new page loads and should theoretically set the desired appearance.
I’d appreciate any help anyone could offer.
Thanks
As already noted in my comment;
Careful when using tag’s set to 0. If no tag is explicitely defined, zero is used. Hence anything that has no tag defined, will match zero.