2013-01-15 42 views
0

我正在研究一个由多个部分组成的大型项目,每个部分都有不同数量的幻灯片。Xcode:使用viewWithTag定位动态创建的按钮时遇到问题

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

创建节时创建导航栏,每载入一张幻灯片时应更新每个按钮的外观。我正在尝试使用viewWithTag来定位每个按钮。当该部分的第一张幻灯片加载时外观没问题,但是当我按下下一个按钮时,一切都消失了。奇怪的是,当再次点击第一张幻灯片时,所有内容都应该显示。

加载节时,将创建一个可变的字典对象数组,其中包含必要的幻灯片信息。我有一个从Main View调用的类方法(- (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方法,理论上应该设置所需的外观。

我会很感激任何人可以提供帮助。

谢谢

+1

小心使用等于0的标签。如果未定义标签,则使用0。所以没有定义标签的任何东西都会在0上匹配。 – Till

+0

感谢您的提示!我将button.tag和buttonDot.tag加1,它按预期工作。再次感谢!! –

回答

0

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

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