0

我正在做一个由几个部分组成的大型项目,每个部分都有不同数量的幻灯片。

我正在尝试动态创建一个导航栏,其中按钮的数量与加载的部分的幻灯片相对应。我遇到的问题是在每张幻灯片加载时设置按钮的开/关外观。

我在创建该部分时创建导航栏,并且每个按钮的外观都应在加载每张幻灯片时更新。我正在尝试使用viewWithTag. 当该部分的第一张幻灯片加载时,外观很好,但是当我点击下一个按钮时,一切都消失了。奇怪的是,当单击返回第一张幻灯片时,一切都按原样显示。

加载一个部分时,会创建一个包含必要幻灯片信息的字典对象的可变数组。我有一个- (void) newbutton:(NSString *) title withIndex:(int) index atPosition:(int) Xpos从主视图调用的类方法 ( ),它通过 for 循环创建自定义“ON”和“OFF”按钮,从 plist 文件传递​​标题和索引参数。之后,加载索引 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.");
}

导航类方法

- (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方法在每次加载新页面时运行,理论上应该设置所需的外观。

任何人都可以提供任何帮助,我将不胜感激。

谢谢

4

1 回答 1

0

正如我在评论中已经指出的那样;

使用标记设置为 0 时要小心。如果没有明确定义标记,则使用零。因此,任何没有定义标签的东西都将匹配零。

于 2013-01-16T13:18:42.573 回答