【问题标题】:issue refreshing uibutton in scrollview在滚动视图中刷新 uibutton 问题
【发布时间】:2012-03-28 22:00:15
【问题描述】:

我尝试刷新一个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) P>

错在哪里? 编辑:现在我只尝试更改titletex。

谢谢!

【问题讨论】:

  • mmmm 我不明白,是iOS 5 应用程序(也适用于iOS 4.3)
  • 如果您将日志语句更改为NSLog(@"View: %@, Tag: %d",[self.scrollView viewWithTag:tag],tag); 它会说明什么?看来您的 (UIButton *) 演员表可能是错误的。
  • for tag = 0 我得到 uiscrollview 而对于其他标签我得到 uibutton,可能是这个问题,我尝试设置另一个标签
  • 问题是tag=0,而不是通知我这个问题!

标签: uiscrollview uibutton refresh


【解决方案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 添加到请求的结果中。这避免了与现有子视图的冲突。

希望对你有帮助。

【讨论】:

  • 感谢您的回答,但问题是当 tag=0,tag=0 是滚动视图,如果我使用标签 1001,1002,... 工作完美:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-08
  • 2011-08-14
  • 2019-04-05
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
相关资源
最近更新 更多