我正在开展一个由多个部分组成的大型项目,每个部分都有不同数量的幻灯片。
我正在尝试动态创建一个导航栏,其中按钮的数量与加载的部分的幻灯片相对应。我遇到的问题是在每张幻灯片加载时设置按钮的开/关外观。
创建部分时创建导航栏,每个幻灯片加载时应更新每个按钮的外观。我正在尝试使用viewWithTag
定位每个按钮。当部分的第一张幻灯片加载时外观很好,但是当我点击下一个按钮时,一切都消失了。奇怪的是,当点击回到第一张幻灯片时,一切都会显示出来。
加载某个部分时,会创建一个包含必要幻灯片信息的可变字典对象数组。我有一个从主视图调用的类方法(- (void) newbutton:(NSString *) title withIndex:(int) index atPosition:(int) Xpos
),它通过for循环创建一个自定义的“ON”和“OFF”按钮,从plist文件传递title和index参数。之后,加载索引0处的幻灯片。
主视图
- (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类方法
- (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;
}
我不确定发生了什么。每次加载新页面时都会运行-(void) setNavAppearance
方法,理论上应该设置所需的外观。
我很感激任何人都可以提供帮助。
由于
答案 0 :(得分:0)
正如我的评论中所述;
使用标记设置为0时要小心。如果没有明确定义标记,则使用零。因此,任何没有定义标签的东西都会匹配零。