在scrollview中发布刷新的uibutton

时间:2012-03-28 22:00:15

标签: uiscrollview uibutton refresh

我尝试刷新一个uibutton的背景图片,这是创建按钮的代码

- (void) disegnaAnteprime {

int x = 2;
int y = 2;
int i = 0;

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 10*103);

for (i=0; i < arrayProgetti.count;i++) {

    NSNumber *indice = [NSNumber numberWithInt:i];

    elemento = [arrayProgetti objectAtIndex:i];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:[elemento objectForKey:@"Titolo"] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(openPdf:) forControlEvents:UIControlEventTouchUpInside];
    [button setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
    button.tag = i;

    NSURL *indirizzoImmagine = [NSURL URLWithString:[elemento objectForKey:@"IndirizzoIcona"]];

    NSLog(@"Indice i: %d",i);

    NSData *imageData = [cache cachedResponseDataForURL:indirizzoImmagine]; 

    if (imageData==nil) {
        [self loadURL:indirizzoImmagine idElementoBottone:indice tipoRichiesta:NO];
    }
    else {
        [button setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];        
    }

    button.frame = CGRectMake(30, y, 190, 190);
    y = y + 192;

    [self.scrollView addSubview:button];
}

}

虽然这是我用于刷新按钮图像的代码

- (void)requestFinished:(ASIHTTPRequest *)request
{
      int tag = [[request.userInfo objectForKey:@"TagBottone"] intValue];        
      NSLog(@"Tag: %d",tag);

      [(UIButton*)[self.scrollView viewWithTag:tag] setTitle:@"hello" forState:UIControlStateNormal];        

}

但我收到此错误

  

[UIScrollView setTitle:forState:]:无法识别的选择器发送到实例0x8179eb0   2012-03-28 23:54:40.477 iVision [2269:15b03] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [UIScrollView setTitle:forState:]:无法识别的选择器发送到实例0x8179eb0'   * 第一次抛出调用堆栈:   (0x1c5f052 0x37f3d0a 0x1c60ced 0x1bc5f00 0x1bc5ce2 0xd574 0x1c60e72 0x37d0b 0x1c60e72 0x12879ef 0x1c3397f 0x1b96b73 0x1b96454 0x1b95db4 0x1b95ccb 0x1df1879 0x1df193e 0x96fa9b 0x3560 0x2d85)

哪里出错? 编辑:现在我只想改变titletex。

谢谢你!

1 个答案:

答案 0 :(得分:1)

这表明你试图在scrollView而不是UIButton中使用setTitle:forState :.您是否尝试过分离代码:

UIButton * btn = (UIButton*)[self.scrollView viewWithTag:tag];
[btn setTitle:@"hello" forState:UIControlStateNormal];

您还可以检查您的演员阵容是否成功:

if([[self.scrollView viewWithTag:tag] isKindOfClass:[UIButton class]])
{
    UIButton * btn = (UIButton*)[self.scrollView viewWithTag:tag];
    [btn setTitle:@"hello" forState:UIControlStateNormal];
}

这一切都表明viewWithTag的结果不是UIButton。 UIScrollView本身有一些子视图。我会以值100开始你的标签然后,只需在请求的结果中加100。这避免了与现有子视图的冲突。

希望它有所帮助。