如何为iPhone中的按钮的特定索引设置背景图像?

发布于 2024-11-09 06:14:40 字数 837 浏览 0 评论 0原文

我已经动态创建了按钮,现在我想为按钮的特定索引设置背景。

这是我的示例片段:

在界面文件中:

 UIButton *answerBtn;
 @property (nonatomic, retain) UIButton *answerBtn;


 for(int i = 0; i < [myArray count]; i++) {

  answerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

 [answerBtn setFrame:CGRectMake(30, x, 260, 40)];

 answerBtn.tag = i; 

 [answerBtn setTitle:[answerList objectAtIndex:i] forState:UIControlStateNormal];
 
 [self.view addSubview: answerBtn];

}

就我而言,我想以不同的方法设置按钮背景。

-(void) custom Method
{
   if(indexValue == correctIndex) // Values are 2
   {
     // so I want to set the background image for the second button

      [answerBtn setBackgroundImage:[UIImage imageNamed:@"selected_correct_answer.png"] forState:UIControlStateNormal]; 

   }
}

但它并没有设置相应的索引,那我该怎么办呢?

I have created buttons dynamically and now I want to set the background for a particular index of the buttons.

Here is my sample snippet:

In the interface file:

 UIButton *answerBtn;
 @property (nonatomic, retain) UIButton *answerBtn;


 for(int i = 0; i < [myArray count]; i++) {

  answerBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

 [answerBtn setFrame:CGRectMake(30, x, 260, 40)];

 answerBtn.tag = i; 

 [answerBtn setTitle:[answerList objectAtIndex:i] forState:UIControlStateNormal];
 
 [self.view addSubview: answerBtn];

}

In my case, I want to set the button background in different methods.

-(void) custom Method
{
   if(indexValue == correctIndex) // Values are 2
   {
     // so I want to set the background image for the second button

      [answerBtn setBackgroundImage:[UIImage imageNamed:@"selected_correct_answer.png"] forState:UIControlStateNormal]; 

   }
}

But it doesn't set the corresponding index, so how can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

围归者 2024-11-16 06:14:40

试试这个

for(int i = 0 ; i < [myArray count] ; i++ )
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTag:i];
    [btn setFrame:CGRectMake(30, x, 260, 40)];
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (IBAction) btnClicked::(id)sender
{
  UIButton *btnTapped=(UIButton *)sender;

for(UIView *btn in  self.view.subviews)
{
    if([btn isKindOfClass:[UIButton class]])
    {   
        UIButton *btnComp = (UIButton*)btn; 

        if(btnComp.tag == btnTapped.tag)
            [btnComp setBackgroundImage:[UIImage imageNamed:@"selected_correct_answer.png" forState:UIControlStateNormal];
        else 
             [btnComp setBackgroundImage:[UIImage imageNamed:@"default_image.png" forState:UIControlStateNormal];


    }


}

}

Try this

for(int i = 0 ; i < [myArray count] ; i++ )
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTag:i];
    [btn setFrame:CGRectMake(30, x, 260, 40)];
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (IBAction) btnClicked::(id)sender
{
  UIButton *btnTapped=(UIButton *)sender;

for(UIView *btn in  self.view.subviews)
{
    if([btn isKindOfClass:[UIButton class]])
    {   
        UIButton *btnComp = (UIButton*)btn; 

        if(btnComp.tag == btnTapped.tag)
            [btnComp setBackgroundImage:[UIImage imageNamed:@"selected_correct_answer.png" forState:UIControlStateNormal];
        else 
             [btnComp setBackgroundImage:[UIImage imageNamed:@"default_image.png" forState:UIControlStateNormal];


    }


}

}

银河中√捞星星 2024-11-16 06:14:40

使用 -(void)custom:(id)sender 而不是 -(void)custom

在发送器中,您可以拥有按钮的索引。

Use -(void)custom:(id)sender instead of -(void)custom.

In the sender you can have the index of the button.

庆幸我还是我 2024-11-16 06:14:40
UIButton *answerBtn = (UIButton *)[self objectWihTag:1]
UIButton *answerBtn = (UIButton *)[self objectWihTag:1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文