0

我有三个名为 tabItem2、tabItem3 和 tabItem4 的 UIButton。tabItems 的标签和标签的文本是动态的,并且取决于 InfoDictionary 中项目的值。我已经根据标签创建了开关,这些标签适用于所有三个按钮。如何更改下面的代码以适应不同的按钮?我需要按钮名称是可变的-> 意思是说 switch 语句应该适用于所有三个按钮。

代码如下所示:

IBOutlet UIButton *tabItem4;
    IBOutlet UIButton *tabItem2;
    IBOutlet UIButton *tabItem3;

IBOutlet UILabel *tabItem4Label;
    IBOutlet UILabel *tabItem2Label;
    IBOutlet UILabel *tabItem3Label;

NSArray *tabItemArray = [self.InfoDictionary objectForKey:@"tabitem"];
    for(int i = 0; i <[tabItemArray count]; i++)
     {
         NSString *nameOfTab = [NSString stringWithFormat:@"tabItem%d",i+2];
         NSLog(@"%@",nameOfTab);
         switch ([[tabItemArray objectAtIndex:i] intValue]) {
             case kTabGameButtonTag:
                 nameOfTab.tag = kTabGameButtonTag;
                 nameOfTabLabel.text = kTabTitleTextGame;
                 [nameOfTab setImage:[UIImage imageNamed:@"game.png"] forState:UIControlStateNormal];
                 break;
             case kTabVideoButtonTag:
                 nameOfTab.tag = kTabVideoButtonTag;
                 nameOfTabLabel.text = kTabTitleTextVideoClip;
                 [nameOfTab setImage:[UIImage imageNamed:@"video.png"] forState:UIControlStateNormal];
                 break;
             case kTabQuizButtonTag:
                 nameOfTab.tag = kTabVideoButtonTag;
                 nameOfTabLabel.text = kTabTitleTextQuiz;
                 [nameOfTab setImage:[UIImage imageNamed:@"quiz.png"] forState:UIControlStateNormal];
                 break;
             default:
                 break;
         }
     }

在这方面需要一些指导。

编辑:

$1 = 0x0a219f50 {
tabitem =     (
        2000,
        2001,
        2002
    );
}

字典中的其余项目是保密的。所以我只分享了重要的信息。

4

2 回答 2

0

经过思考,我这样解决了这个问题:

 NSArray *tabItemArray = [self.InfoDictionary objectForKey:@"tabitem"];
    NSMutableArray *tabItems = [NSMutableArray arrayWithObjects:tabItem2,tabItem2Label,tabItem3,tabItem3Label,tabItem4,tabItem4Label,nil];
    for(int i = 0,j = 0 ; i <[tabItemArray count]; i++, j=j+2)
     {
         UIButton  *item1 =  [tabItems objectAtIndex:j];
         UILabel  *label1 = [tabItems objectAtIndex:j+1];
         switch ([[tabItemArray objectAtIndex:i] intValue]) {
             case kTabGameButtonTag:
                 item1.tag = kTabGameButtonTag;
                 label1.text = kTabTitleTextGame;
                 [item1 setImage:[UIImage imageNamed:@"game.png"] forState:UIControlStateNormal];
                 break;
             case kTabVideoButtonTag:
                 item1.tag = kTabVideoButtonTag;
                 label1.text = kTabTitleTextVideoClip;
                 [item1 setImage:[UIImage imageNamed:@"video.png"] forState:UIControlStateNormal];
                 break;
             case kTabQuizButtonTag:
                 item1.tag = kTabVideoButtonTag;
                 label1.text = kTabTitleTextQuiz;
                 [item1 setImage:[UIImage imageNamed:@"quiz.png"] forState:UIControlStateNormal];
                 break;
             default:
                 break;
         }
     }
于 2013-03-09T16:05:23.833 回答
0

我认为您正在设置图像而不是按钮的背景图像,请尝试使用此代码。我认为不需要另一个标签来显示按钮的标题

[nameOfTab setImage:[UIImage imageNamed:@"game.png"] 
           forState:UIControlStateNormal];

换成这个

[nameOfTab setBackgroundImage:[UIImage imageNamed:@"game.png"] 
                     forState:UIControlStateNormal];
于 2013-03-09T16:27:07.517 回答