在iOS中更改运行时的UIButton名称

时间:2013-03-09 13:56:24

标签: ios objective-c uibutton

我有三个名为tabItem2,tabItem3和tabItem4的UIButtons。 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
    );
}

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

2 个答案:

答案 0 :(得分: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;
         }
     }

答案 1 :(得分:0)

我认为您正在设置图片而不是按钮的backgroundImage尝试此代码。我不认为只需要另一个标签来显示按钮的标题

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

替换为

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