【问题标题】:problem in Custom Button Action in iPhone sdkiPhone sdk 中的自定义按钮操作中的问题
【发布时间】:2011-05-16 11:05:47
【问题描述】:

嗨,

在我的应用程序中,我在 scrollView 中显示一个列表。我已经针对每个项目放置了自定义 btn 在滚动视图中。在 btn 操作中,我必须将未选择的图像更改为选定的图像。但是当我点击第一个 btn 时,最后一个 btn 图像被更改。

代码:

    - (void)reLoadView 
      {

    float yCoordinateStartLocation = 20;
    float xCoordinateStartLocatioForItem = 80;
    float xCoordinateStartLocatioForButton=30;
    float questionWidth =100;
    UIFont* questionFont = [UIFont boldSystemFontOfSize:19];
    UIColor *color = [UIColor colorWithRed:0.0 green:54.0/255.0 blue: 129.0/255.0 alpha:1.0];
    UIColor *bgcolor = [UIColor clearColor];

    for(int i=0; i<[itemArray count]; i++)
    {
    showItems *showItem = [itemArray objectAtIndex:i];

     float itemHeight = [ShowViewController calculateHeightOfTextFromWidth:showItem.itemName    :questionFont : questionWidth :UILineBreakModeCharacterWrap];


    itemLabel=[[UILabel alloc]init];
    itemLabel.frame=CGRectMake(xCoordinateStartLocatioForItem,yCoordinateStartLocation,questionWidth,40);
    itemLabel.text =showItem.itemName;
    itemLabel.textColor = color;
    itemLabel.font = questionFont;
    itemLabel.numberOfLines = 0;
    itemLabel.lineBreakMode = UILineBreakModeCharacterWrap;
    itemLabel.textAlignment=UITextAlignmentLeft;
    itemLabel.backgroundColor = bgcolor;
    [scrollView addSubview:itemLabel];


    UIImage *unSelectedImage = [UIImage imageNamed:@"unChecked.png"];
    checkBoxBttn = [[MyCustomButton alloc] initWithIdValue:showItem.itemName tagValue:tagCount];
    //checkBoxBttn = [[MyCustomButton alloc] initWithIdValue:showItem.itemName];

    CGRect frame = CGRectMake(xCoordinateStartLocatioForButton,yCoordinateStartLocation, 30, 30);
    [checkBoxBttn setFrame:frame];
    checkBoxBttn.tag=tagCount;


    [checkBoxBttn setBackgroundImage:unSelectedImage forState:UIControlStateNormal];
    [checkBoxBttn setShowsTouchWhenHighlighted:YES];
    [checkBoxBttn addTarget:self action:@selector(checkButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:checkBoxBttn];
    tagCount++;


    yCoordinateStartLocation = yCoordinateStartLocation + itemHeight + 20;

    }

      scrollView.contentSize = CGSizeMake(320, yCoordinateStartLocation+50);
      }



       -(void)checkButtonAction:(MyCustomButton*)sender
{
    NSString *sndStr = [sender itemName];
    //taglabel=[sender tagNumber];
    //NSString *tagNo=[NSString stringWithFormat:@"%d",taglabel];



    for(int i=0; i<[itemArray count]; i++)
    {
    showItems *showItem = [itemArray objectAtIndex:i];


    if([sndStr isEqualToString:showItem.itemName])
    {
            printf("\n ====     checkBoxBttn.tag:%d",   checkBoxBttn.tag);
    UIImage *selectedImage = [UIImage imageNamed:@"checked.png"];
    [checkBoxBttn setBackgroundImage:selectedImage forState:UIControlStateNormal];

    }
    }

   }

任何人都可以帮助我。 提前致谢。

【问题讨论】:

  • 你在代码中哪里定义了 checkBoxBttn?

标签: iphone objective-c


【解决方案1】:

嗯,无法弄清楚你在这里想要达到什么目的。在某些时候,我觉得 UITableView 可能适合您的需求。

点击第一个按钮时最后一个按钮发生变化的原因是checkBoxBttn 被设置为-reLoadView 中循环结束时的最后一个按钮,所以当你在-checkButtonAction: 中再次引用它时,它指的是到最后一个按钮,而不是接收用户操作的按钮。

假设您要更改接收用户操作的按钮的图像,您可能需要使用以下代码 -

-(void)checkButtonAction:(MyCustomButton*)sender
{
    UIImage *selectedImage = [UIImage imageNamed:@"checked.png"];
    [sender setBackgroundImage:selectedImage forState:UIControlStateNormal];
}

【讨论】:

  • 当然,这不是用来翻转的。翻转将涉及额外的工作。
【解决方案2】:

这行有问题 [checkBoxBttn setBackgroundImage:selectedImage forState:UIControlStateNormal];

你必须通过标记更新 checkBoxBttn 背景图像

【讨论】:

    猜你喜欢
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多