【问题标题】:how to get current uilabel text from uiscroll view runtime如何从 uiscroll 视图运行时获取当前的 uilabel 文本
【发布时间】:2012-03-23 15:19:24
【问题描述】:

我的任务是开发带有单词计数的应用程序,并在运行时生成的 uiscrollview 中显示,并将 uilabel 作为子视图。

这个过程就像当用户加载页面时,1000个单词将用滚动视​​图填充rutime生成的uilabel。所有东西都是从运行时设置的。但它是我们的应用程序,我们有一个按钮可以在按钮单击时添加当前的 uilabel 文本。

这些词是从数据库中随机出现的。当我点击按钮时,它给了我不同的词。从滚动视图显示的单词。

以下是我用滚动视图填充 uilabel 中的数据的代码:

wordName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(searchStatement, 2)];
                [wordNameArray addObject:wordName];
int i = 0;
    for (i = 1; i <= kNumImages; i++)
    {
        randomN = [wordNameArray objectAtIndex:arc4random() % [wordNameArray count]];

        UIImage *image = [UIImage imageNamed:@"word_bg.png"];
        word = [[UILabel alloc]initWithFrame:CGRectMake(15,12,250,120)];
        [word setFont:[UIFont fontWithName:@"Helvetica-Bold" size:36.0f]];
        word.textColor = [UIColor whiteColor];
        word.lineBreakMode = UILineBreakModeWordWrap;
        word.numberOfLines = 2;
        word.textAlignment = UITextAlignmentCenter;
        word.backgroundColor = [UIColor clearColor];
        [word setText:randomN];
        lblWord.text = word.text;

        word.tag = i;
        NSLog(@"tag no:%d",word.tag);

        imageView = [[UIImageView alloc] initWithImage:image];
        [imageView addSubview:word];

        CGRect rect = imageView.frame;
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
        imageView.frame = rect;
        imageView.tag = i;  

        //NSLog(@"%@",word.text);
        //NSLog(@"%@",lblWord.text);

        //  [scrollView1 addSubview:l];
        [scrollView1 addSubview:imageView];

        touchToSee.hidden = TRUE;
        [imageView release];
    }

    [self layoutScrollImages];

以下是我从标签中获取单词的滚动事件,但它给了我数组中的最后一个单词:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    touchToSee.hidden = TRUE;
    NSLog(@"word-%@",word.text);

}

当我点击按钮时,我没有得到完美的工作,因此以下是我的代码:

NSString *getword = [NSString stringWithFormat:@"%@",word.text];
    NSLog(@"lblCountdownBackward:%@",word.text);

所以请帮助我并尽可能提供一些示例代码。

提前致谢。

【问题讨论】:

    标签: iphone uiscrollview uilabel


    【解决方案1】:

    您似乎正在创建许多单词对象,并将它们添加到屏幕上。

    在您的 for 循环中,您将代码执行到 kNumImages。并且在每次运行中,word 对象都会被替换为一个新对象。

    所以当你稍后得到这个词时,它将是你创建的最后一个 word 对象(因此是最后一个词)。

    要解决这个问题,您可以使用NSMutableArray 并继续向其中添加新的UILabel 对象(而不是创建word 对象)。然后再做类似的事情

    [array objectAtIndex:0].text;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      相关资源
      最近更新 更多