2013-01-15 60 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時要小心。如果沒有標籤被明確定義,則使用零。因此,任何沒有定義標籤的東西都會匹配零。